代码示例: 以下代码中,创建了一个名为“users”的数据表,列名分别为“_id”和“name”。如果存在SQL Lite数据库错误,可以参考代码检查问题。
public class DBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "mydatabase.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "users";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_NAME = "name";
private static final String CREATE_TABLE = "create table " + TABLE_NAME + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_NAME + " text not null);";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}