我希望能够使用android在sqlite中添加数据。 列Id应该具有主键,并且应该是自动增量字段。 并且状态的默认值应为0.不确定这是否为sqlite执行此操作。
public static final String DATABASE_NAME="tododata";
public static final String TABLE_NAME="todolist";
public static final int KEY_ID= 0;
public static final String KEY_TITLE="Title";
public static final String KEY_DESCRIPTION="Description";
// public static final Date KEY_DATE = ;
public static final int KEY_STATUS = 0;
public static final int DATABASE_VERSON=1;
public DataHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSON);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table "+TABLE_NAME+"(Id integer primary key not null autoincrement 1 , title text not null, description text not null, date date not null, status int default 0);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists "+TABLE_NAME);
}
答案 0 :(得分:1)
这应该是正确的语法:
CREATE TABLE table_name (Id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, description TEXT NOT NULL, date DATE NOT NULL, status INTEGER DEFAULT 0)
答案 1 :(得分:0)
您的查询应该采用以下格式: -
sqliteDatabase.execSQL("create table tableName ("+COL1_NAME+" integer primary key autoincrement, "+COL2_NAME+" text , "+COL3_NAME+" text , "+COL4_NAME+" text)");