如何在Android SQLite数据库上制作AUTO_INCREMENT?

rog*_*gcg 19 sqlite android

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

这是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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

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

this.dh.insert(1 , 1, contactName, numType, contactNumber,
                    duration, callDate, currTime);
Run Code Online (Sandbox Code Playgroud)

Jib*_*ibW 22

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

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

如果INT在创建表时已定义主键字段,则无效.应该是INTEGER这就是它.

当您在插入记录时未传递主键字段的任何值时,它会自动将该值增加为唯一值(MYSQL AUTO_INCREMENT的工作方式相同)

  • 我认为JibW只是与MySQL进行比较以更好地解释事情. (6认同)
  • 在问题的任何地方似乎都没有提到MySQL. (4认同)
  • 哈哈,那是我的问题.我宣布它为INT;)谢谢. (3认同)
  • 只是添加一个关于为什么应该避免**AUTO_INCREMENT**语句的注释.`AUTOINCREMENT关键字会产生额外的CPU,内存,磁盘空间和磁盘I/O开销,如果不是严格需要,应该避免使用.通常不需要它.https://www.sqlite.org/autoinc.html.使用**INTEGER PRIMARY KEY**就足够了. (2认同)

Cri*_*ian 13

你不必解析任何东西.如果列创建为AUTOINCREMENT,则只传递其他值:

db.execSQL("insert into "
        + TABLE_NAME
        + "(contact_id, contact_name, number_type, contact_number, duration, date, current_time, cont) "
        + "values( "+ cId + ", " + cName + ", " + numType + ", "
        + cNum + ", " + dur + ", " + date + ", " + currTime + ", ? )");
Run Code Online (Sandbox Code Playgroud)

顺便说一句,总是建议使用Android的insert方法插入数据:

ContentValues values = new ContentValues();
values.put("contact_id", cId);
values.put("contact_name", cName);
// etc.
db.insert(TABLE_NAME, null, values);
Run Code Online (Sandbox Code Playgroud)