如何使AUTO_INCREMENT在Android SQLite数据库? [英] How to make AUTO_INCREMENT on Android SQLite database?

查看:102
本文介绍了如何使AUTO_INCREMENT在Android SQLite数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我想创建一个AUTO_INCREMENT列的数据库。但我不知道如何分析的方法插入值。我只是不知道该怎么解析到一个AUTO_INCREMENT说法,而我解析1,其中应AUTO_INCREMENT,但我知道它不是我应该解析。

下面是 CallDatHelper.java 类,我宣布方法插入和创建数据库的方法。

 包com.psyhclo;

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

进口的java.util.ArrayList;
进口的java.util.List;

公共类CallDataHelper {

私有静态最后弦乐DATABASE_NAME =calls.db;
私有静态最终诠释DATABASE_VERSION = 1;
私有静态最后弦乐TABLE_NAME =contact_data;

私人上下文的背景下;
私人SQLiteDatabase分贝;

公共CallDataHelper(上下文的背景下){
    this.context =背景;
    OpenHelper openHelper =新OpenHelper(this.context);
    this.db = openHelper.getWritableDatabase();
}

公共布尔插入(CID整数,字符串CNAME,字符串numType,
        字符串CNUM,字符串DUR,字符串日期,字符串currTime){
    this.db.execSQL(插入
            + TABLE_NAME
            +(同上,CONTACT_ID,CONTACT_NAME,NUMBER_TYPE,contact_number,时间,日期,CURRENT_TIME,续)
            +的值(?,+ CID +,+ CNAME +,+ numType +,
            + CNUM +,+ DUR +,+日期+,+ currTime +,)?);
    返回true;
}

公共无效atualiza(串词){
    this.db.execSQL(更新字数设置续=续+ 1 WHERE(字=?),
            新的String [] {文字});
}

公共无效用deleteAll(){
    this.db.delete(TABLE_NAME,NULL,NULL);
}

公共布尔选择(字符串WRD){

    串词;
    光标光标= this.db.query(TABLE_NAME,新的String [] {字},
            字怎​​么样?,新的String [] {} WRD,NULL,NULL,NULL);
    如果(cursor.moveToFirst()){
        做 {
            字= cursor.getString(0);
        }而(cursor.moveToNext());
    }
    如果(光标=空&安培;!&安培;!cursor.isClosed()){
        cursor.close();
        返回true;
    } 其他 {
        返回false;
    }
}

公开名单<字符串>全选() {
    名单<字符串>名单=新的ArrayList<字符串>();
    光标光标= this.db.query(TABLE_NAME,新的String [] {字},
            NULL,NULL,NULL,NULL,续递减);
    如果(cursor.moveToFirst()){
        做 {
            list.add(cursor.getString(0).toUpperCase());
        }而(cursor.moveToNext());
    }
    如果(光标=空&安培;!&安培;!cursor.isClosed()){
        cursor.close();
    }
    返回列表;
}

私有静态类OpenHelper扩展SQLiteOpenHelper {

    OpenHelper(上下文的背景下){
        超(背景下,DATABASE_NAME,空,DATABASE_VERSION);
    }

    @覆盖
    公共无效的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),持续时间,日期DATE,TIME CURRENT_TIME,续INTEGER));

    }

    @覆盖
    公共无效onUpgrade(SQLiteDatabase分贝,INT oldVersion,诠释静态网页){
        Log.w(RatedCalls数据库,
                升级数据库,这将删除表并重新创建。);
        db.execSQL(DROP TABLE IF EXISTS+ TABLE_NAME);
        的onCreate(DB);
    }
}
}
 

和这里就是我称之为插入方式解析我想插入的数据。

  this.dh.insert(1,1,联系人姓名,numType,contactNumber,
                    期间,callDate,currTime);
 

解决方案

您不必解析什么。如果列创建为 AUTOINCREMENT ,只是通过其他值:

  db.execSQL(插入
        + TABLE_NAME
        +(CONTACT_ID,CONTACT_NAME,NUMBER_TYPE,contact_number,时间,日期,CURRENT_TIME,续)
        +的值(+ CID +,+ CNAME +,+ numType +,
        + CNUM +,+ DUR +,+日期+,+ currTime +,)?);
 

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

  ContentValues​​值=新ContentValues​​();
values​​.put(CONTACT_ID,CID);
values​​.put(CONTACT_NAME,CNAME);
// 等等。
db.insert(TABLE_NAME,空,价值观);
 

Hey I want to create a database with an AUTO_INCREMENT column. But I don't know how to parse the value in the method insert. I just don't know what to parse to an AUTO_INCREMENT argument, and I parsed 1 where should be auto_increment, but I know its not that I should parse.

Here is the CallDatHelper.java class where I declare the method insert, and the method that creates the database.

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);
    }
}
}

And here is where I call the insert method parsing the data I want to insert.

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

解决方案

You don't have to parse anything. If the column was created as AUTOINCREMENT, just pass the other values:

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 + ", ? )");

By the way, it's always recommended to insert data using the Android's insert method:

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

这篇关于如何使AUTO_INCREMENT在Android SQLite数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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