首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android SQLite数据库上制作AUTO_INCREMENT?

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

Stack Overflow用户
提问于 2010-12-08 22:18:34
回答 5查看 68.7K关注 0票数 20

嘿,我想创建一个包含AUTO_INCREMENT列的数据库。但是我不知道如何解析方法insert中的值。我只是不知道如何解析AUTO_INCREMENT参数,我解析了1,其中应该是auto_increment,但我知道这不是我应该解析的。

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

代码语言:javascript
复制
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 方法来解析我想要插入的数据。

代码语言:javascript
复制
this.dh.insert(1 , 1, contactName, numType, contactNumber,
                    duration, callDate, currTime);
EN

回答 5

Stack Overflow用户

回答已采纳

发布于 2010-12-08 22:21:21

你不需要解析任何东西。如果该列是作为AUTOINCREMENT创建的,则只需传递其他值:

代码语言:javascript
复制
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 + ", ? )");

顺便说一下,我们总是推荐使用安卓的insert方法来插入数据:

代码语言:javascript
复制
ContentValues values = new ContentValues();
values.put("contact_id", cId);
values.put("contact_name", cName);
// etc.
db.insert(TABLE_NAME, null, values);
票数 13
EN

Stack Overflow用户

发布于 2012-02-15 20:52:49

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

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

如果在创建表时使用INT定义主键字段,它将不起作用。应该是INTEGER,仅此而已。

如果在插入记录时没有为主键字段传递任何值,则会自动将该值增加为唯一值(与MYSQL AUTO_INCREMENT的工作方式相同)

票数 23
EN

Stack Overflow用户

发布于 2010-12-08 22:26:05

代码语言:javascript
复制
this.db.insert(NULL , 1, contactName, numType, contactNumber,
                    duration, callDate, currTime);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4388459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档