Android - SQLite AUTOINCREMENT不递增主键

时间:2014-02-08 08:19:17

标签: android sqlite auto-increment

我搜索了堆栈溢出,无法找到解决方案。 这是我对SQLite的onCreate代码。

public void onCreate(SQLiteDatabase database) {
    String query = "CREATE TABLE cards (cardID INTEGER PRIMARY KEY AUTOINCREMENT, question VARCHAR, answer VARCHAR, subject VARCHAR)";
    database.execSQL(query);
}

我用它从适配器插入(DbTools)

public void insertCard(HashMap<String, String> queryValues) {

    SQLiteDatabase database = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put("question", queryValues.get("question"));
    values.put("answer", queryValues.get("answer"));
    values.put("subject", queryValues.get("subject"));

    database.insert("cards", null, values);
    database.close();

}

这是我将数据插入数据库的方式。

HashMap<String, String> Values = new HashMap<String, String>(); 
Values.put("question", newQuestion);
Values.put("answer", newAnswer);
Values.put("subject", newSubject);
dbTools.insertCard(Values);

其中newQuestion,newAnswer和newSubject是定义来自edittext字段的用户输入的变量。

当我通过调用toast来检查数据库时,除了插入为Null的cardID之外,所有内容都会被插入。 怀疑我的吐司检查,我下载了一个数据库查看器,并发现cardID为空。

有什么建议吗?

3 个答案:

答案 0 :(得分:2)

cardID INTEGER PRIMARY KEY AUTOINCREMENT替换为cardID INTEGER PRIMARY KEY

public void onCreate(SQLiteDatabase database) {
    String query = "CREATE TABLE cards (cardID INTEGER PRIMARY KEY, question VARCHAR, answer VARCHAR, subject VARCHAR)";
    database.execSQL(query);
}

您不必像在SQLite中那样专门写AUTO_INCREMENT

只需将主键字段定义为_id INTEGER PRIMARY KEY

答案 1 :(得分:2)

试试这个:

integer primary key autoincrement not null

然后它将是:

String query = "CREATE TABLE cards (cardID integer primary key autoincrement not null, question   VARCHAR, answer VARCHAR, subject VARCHAR)";

修改

只需查看我的代码:

import java.util.ArrayList; 
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class AABDatabaseManager
{
Context context;
private SQLiteDatabase db;
private final String DB_NAME = "MY_DB";
private final int DB_VERSION = 1;

public AABDatabaseManager(Context context)
{       
            this.context = context;
    CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
    this.db = helper.getWritableDatabase();
}

public void addRow(String StringOne, String StringTwo)
{   
    ContentValues values = new ContentValues();
    values.put("Username", StringOne);
    values.put("Passwrd", StringTwo);
    try{
              db.insert("My_table", null, values);
            }
    catch(Exception e)
    {
        Log.e("DB ERROR", e.toString());
        e.printStackTrace();
    }
}

private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
    public CustomSQLiteOpenHelper(Context context)
    {
        super(context,DB_NAME,null,DB_VERSION);
    }


    public void onCreate(SQLiteDatabase db)
    {
        String query = "CREATE TABLE My_table(ID integer primary key autoincrement not null, Username text, Passwrd text)";                         ");";       
        db.execSQL(query);
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }
}

插入如下数据:

AABDatabaseManager db = new AABDatabaseManager(this);
db.addRow(Stringvalue1,Stringvalue2);

答案 2 :(得分:0)

我已阅读(在Android Studio Development Essentials中),Android对主键的名称很挑剔,并希望它始终为_id

此外,根据How to make AUTO_INCREMENT on Android SQLite database?隐含的自我增量是应该避免的。

因此,请尝试使用以下内容:

public void onCreate(SQLiteDatabase database) {
    String query = "CREATE TABLE cards (_ID INTEGER PRIMARY KEY, question VARCHAR, answer VARCHAR, subject VARCHAR)";
    database.execSQL(query);
}