android - Android 中的 Sqlite 查询自动递增和默认值

标签 android sqlite android-sqlite

我希望能够使用 android 在 sqlite 中添加数据。 列 Id 应该有主键并且应该是一个自动递增字段。 status 应该有一个默认值 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);
}

最佳答案

这应该是正确的语法:

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)

关于android - Android 中的 Sqlite 查询自动递增和默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45354949/

相关文章:

java - 在 SQLiteDatabase 查询方法中使用 DISTINCT 时如何不使用 LIMIT?

java - 如何在 Objectify 上返回自定义对象列表

java - libGDX - 添加分数并将其显示在屏幕的左上角?

android - 仅在 Android MediaPlayer 中播放 youtube 视频的音频部分

sqlite - 使用 Nim 连接到 SQLite 数据库

sqlite - 日期参数在Delphi SQLite查询中误读

android - 正确插入和从 sqlite android 获取数据

android - 使用 fragment 时如何管理应用栏?

ruby-on-rails - 如何在Rails控制台中通过脚本测试SQLite数据库?

android - 数据库是否存在【如何调用类】