Android使用sqlite 实例

来源:互联网 发布:手机移动数据不能上网 编辑:程序博客网 时间:2024/05/16 07:30

package com.xiaoke.accountsoft.dao;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DBOpenHelper extends SQLiteOpenHelper {private static final int VERSION = 1;// 定义数据库版本号private static final String DBNAME = "account.db";// 定义数据库名public DBOpenHelper(Context context){// 定义构造函数super(context, DBNAME, null, VERSION);// 重写基类的构造函数}@Overridepublic void onCreate(SQLiteDatabase db){// 创建数据库db.execSQL("create table tb_outaccount (_id integer primary key,money decimal,time varchar(10),"+ "type varchar(10),address varchar(100),mark varchar(200))");// 创建支出信息表db.execSQL("create table tb_inaccount (_id integer primary key,money decimal,time varchar(10),"+ "type varchar(10),handler varchar(100),mark varchar(200))");// 创建收入信息表db.execSQL("create table tb_pwd (password varchar(20))");// 创建密码表db.execSQL("create table tb_flag (_id integer primary key,flag varchar(200))");// 创建便签信息表}@Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)// 覆写基类的onUpgrade方法,以便数据库版本更新{}}

2 在新建这个对象的时候就会自动建立数据库 andsqlite.db

在DDMS的File Explorer   的data/data/你的包名/database下面3 调用方法

        SqlDB db = new SqlDB(MainActivity.this);        SQLiteDatabase sqldb = db.getWritableDatabase();        sqldb.execSQL("select * from mytest");



原创粉丝点击