android - 如何在 Android SQLite 数据库上创建 AUTO_INCREMENT?

标签 android sqlite

嘿,我想创建一个带有 AUTO_INCREMENT 列的数据库。但我不知道如何解析方法插入中的值。我只是不知道将什么解析为 AUTO_INCREMENT 参数,我解析了应该是 auto_increment 的 1,但我知道它不应该解析。

这里是 CallDatHelper.java 类,我在其中声明方法 insert 和创建数据库的方法。

package com.psyhclo;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteStatement;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

public class CallDataHelper {

private static final String DATABASE_NAME = "calls.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "contact_data";

private Context context;
private SQLiteDatabase db;

public CallDataHelper(Context context) {
    this.context = context;
    OpenHelper openHelper = new OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
}

public boolean insert(Integer cId, String cName, String numType,
        String cNum, String dur, String date, String currTime) {
    this.db.execSQL("insert into "
            + TABLE_NAME
            + "(id, contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
            + "values( ? ," + cId + ", " + cName + ", " + numType + ", "
            + cNum + ", " + dur + ", " + date + ", " + currTime + ", ? )");
    return true;        
}

public void atualiza(String word) {
    this.db.execSQL("UPDATE words SET cont = cont + 1 WHERE (word= ?)",
            new String[] { word });
}

public void deleteAll() {
    this.db.delete(TABLE_NAME, null, null);
}

public boolean select(String wrd) {

    String word;
    Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" },
            "word like ?", new String[] { wrd }, null, null, null);
    if (cursor.moveToFirst()) {
        do {
            word = cursor.getString(0);
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
        return true;
    } else {
        return false;
    }
}

public List<String> selectAll() {
    List<String> list = new ArrayList<String>();
    Cursor cursor = this.db.query(TABLE_NAME, new String[] { "word" },
            null, null, null, null, "cont desc");
    if (cursor.moveToFirst()) {
        do {
            list.add(cursor.getString(0).toUpperCase());
        } while (cursor.moveToNext());
    }
    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    return list;
}

private static class OpenHelper extends SQLiteOpenHelper {

    OpenHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL("CREATE TABLE "
                + TABLE_NAME
                + "(id INTEGER PRIMARY KEY AUTOINCREMENT, contact_id INTEGER, contact_name VARCHAR(50), number_type VARCHAR(50), contact_number VARCHAR(50), duration TIME, date DATE, current_time TIME, cont INTEGER)");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w("RatedCalls Database",
                "Upgrading database, this will drop tables and recreate.");
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
        onCreate(db);
    }
}
}

这里是我调用 insert 方法解析我想要插入的数据的地方。

this.dh.insert(1 , 1, contactName, numType, contactNumber,
                    duration, callDate, currTime);

最佳答案

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

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

如果在创建表时用INT定义了主键字段,它将不起作用。它应该是 INTEGER 就是这样。

当插入记录时主键字段不传任何值时,会自动将该值增加为唯一值(同MYSQL AUTO_INCREMENT有效)

关于android - 如何在 Android SQLite 数据库上创建 AUTO_INCREMENT?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4388459/

相关文章:

android - 如何向多个 WidgetProvider 广播 Intent

mysql - Rails 中不区分大小写的 group_by?

android - 用户输入的撇号使 android 应用程序崩溃(与 SQL 相关)

android - 为 chromecast 设置 cors header

java - WakefulBrodcastReceiver/Alert 不响(Wakelock?进程被杀死?)

javascript - 清理一大段 Javascript 代码

python - flask +Sqlite : Strings that are integers return as type int?

java - 无法在 Android Studio 中使用 BaseGameActivity

python-2.7 - 带有 LIKE 搜索的 Python Tkinter 自动完成组合框?

Python3 - 有没有一种方法可以在一个非常大的 SQlite 表上逐行迭代,而无需将整个表加载到本地内存中?