2010-12-08 61 views
18

嘿,我想创建一个AUTO_INCREMENT列的数据库。但我不知道如何解析方法插入中的值。我只是不知道要解析AUTO_INCREMENT参数,我解析了1应该是auto_increment,但我知道它不是我应该解析的。如何在Android SQLite数据库上创建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); 
    } 
} 
} 

而这里就是我所说的插入方法解析我想插入的数据。

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

让我做一个安全的话在这里:手动构建这样的SQL查询是一个肯定的配方砍死很辛苦。如果攻击者决定任何这些参数的值,则可以完全修改该查询。在这里看到更多的信息:http://en.wikipedia.org/wiki/SQL_injection – MarioVilas 2012-04-28 09:11:00

回答

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

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

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

但如何在另一个类中实现这个?更好的是,如何在另一个类上调用此方法,只需引用它并解析正确的参数? – rogcg 2010-12-08 14:40:04

+0

那么......你已经有了一个方法,只是用它......有什么问题? – Cristian 2010-12-08 14:45:12

+0

我说,必须在活动权限上调用db.insert方法?但为了调用这个方法,我需要声明对象db,它是一个SQLiteDatabase对象。但是,如何在活动中使其失去意义呢?我的意思是,私人SQLiteDatabase db ='what ???'。 – rogcg 2010-12-08 14:49:21

1

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

21

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

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

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

当你不及格的申请插入记录时,它会自动增加值是唯一值(同样的方式MYSQL AUTO_INCREMENT作品)

1

最终静态字符串DATABASE_NAME =“empDb主键的任何值。D b”;

public DatabaseHelper(Context context) { 
    super(context, Database_name, null, 1); 
} 
@Override 
public void onCreate(SQLiteDatabase db) { 
    db.execSQL("create table emp_tbl (id INTEGER PRIMARY KEY AUTOINCREMENT,name TEXT,salary TEXT)"); 
} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
    db.execSQL("DROP TABLE IF EXISTS tbl_emp"); 
} 

获取更多信息 https://www.youtube.com/watch?v=W8-Z85oPNmQ 博客:https://itmulc.blogspot.com/2016/08/android-sqlite-database-with-complete.html