Android的 - SQLite的AUTOINCREMENT不是递增的主键 [英] Android - SQLite AUTOINCREMENT not incrementing primary key

查看:169
本文介绍了Android的 - SQLite的AUTOINCREMENT不是递增的主键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已搜索周围的堆栈溢出,但没有找到一个解决方案。 这是我的onCreate code的SQLite的。

I've searched around stack overflow and couldn't find a solution for this. This is my onCreate code for SQLite.

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)

I use this to insert from the adapter (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();

}

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

This is the way I am inserting data into the database.

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字段定义用户输入变量。

Where newQuestion, newAnswer and newSubject are variables that define the user input from an edittext field.

当我通过调用敬酒检查数据库,一切都只是插入其插入为空的cardID。 怀疑我敬酒检查,我下载了一个数据库浏览器,也发现cardID为null。

When I check the database by calling a toast, everything is inserted except the cardID which is inserted as Null. Doubting my toast check, I downloaded a database viewer and also found that the cardID was null.

有什么建议?

推荐答案

试试这个:

integer primary key autoincrement not null

那么这将是:

Then it will be:

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

修改

只是检查我的code:

Just check my code:

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)
    {

    }
}

这样的插入数据:

Insert data like this:

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

这篇关于Android的 - SQLite的AUTOINCREMENT不是递增的主键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆