创建表的语句很简单,格式如下
create table 表名(字段名 类型,字段名 类型,…);
package com.uyac.andriodsqlite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ShaoQuanwei on 2017/2/15.
*/
public class MySqliteHelper extends SQLiteOpenHelper {
/*表名*/
private final String TABLE_NAME_PERSON = "person";
/*id字段*/
private final String VALUE_ID = "_id";
private final String VALUE_NAME = "name";
private final String VALUE_ISBOY = "isboy";
private final String VALUE_AGE = "age";
private final String VALUE_ADDRESS = "address";
/*头像字段*/
private final String VALUE_PIC = "pic";
/*创建表语句 语句对大小写不敏感 create table 表名(字段名 类型,字段名 类型,…)*/
private final String CREATE_PERSON = "create table " + TABLE_NAME_PERSON + "(" +
VALUE_ID + " integer primary key," +
VALUE_NAME + " text ," +
VALUE_ISBOY + " integer," +
VALUE_AGE + " ingeter," +
VALUE_ADDRESS + " text,"+
VALUE_PIC + " blob"+
")";
public MySqliteHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//创建表
db.execSQL(CREATE_PERSON);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
mySqliteHelper = new MySqliteHelper(context,Constants.DB_NAME,null,Constants.DB_VERSION);
当你new了之后,添加第一条数据时,才可以在data/data/包名/databases目录下面可以发现你创建的数据库名和表名。
- 当首次安装时
02-17 10:41:01.187 25862-25862/com.uyac.andriodsqlite E/MySqliteHelper: -------> MySqliteHelper
02-17 10:41:01.480 25862-25862/com.uyac.andriodsqlite E/MySqliteHelper: -------> onCreate
- 当再次启动数据库版本保持不变时
02-17 10:45:52.976 29607-29607/com.uyac.andriodsqlite E/MySqliteHelper: -------> MySqliteHelper
- 当再次安装数据库版本升级时
02-17 10:41:59.286 27082-27082/com.uyac.andriodsqlite E/MySqliteHelper: -------> MySqliteHelper
02-17 10:41:59.421 27082-27082/com.uyac.andriodsqlite E/MySqliteHelper: -------> onUpgrade
sqlite也无非就创建、增、删、改、查、更新。