插入数据库时​​遇到麻烦

时间:2013-02-14 05:41:19

标签: android

我试图向数据库插入一些值,它显示没有找到这样的表的异常。

我在OpenHelper类中用于创建数据库的代码是

//Call Details Table
static final String Tablenamecall = "call_details";
static final String key_idcall = "key_idcall";
static final String datecall = "datecall";
static final String timecall = "timecall";
static final String toaddcall = "toaddcall";

查询表创建

public void onCreate(SQLiteDatabase db) 
{

    db.execSQL("CREATE TABLE "+Tablenamecall+"("+key_idcall+" INTEGER PRIMARY KEY, "+datecall+" TEXT, "+timecall+" TEXT, "+toaddcall+" TEXT)");

}

插入表格的代码是

    //Call Details Insertion Here

public void calldetails_insertion(String date11 ,String time11 ,String to11) 
{
    SQLiteDatabase db=this.getWritableDatabase();
    ContentValues cv=new ContentValues();

     cv.put(datecall, date11);
     cv.put(timecall, time11);
     cv.put(toaddcall, to11);


     db.insert(Tablenamecall, datecall, cv);


     db.close();
     Log.v("DB", "Inset_User OK"); 
}

我在活动中使用的代码是

        OpenHelper db_obj =  new OpenHelper(context);
            db_obj.calldetails_insertion(dateString, timeString,phoneNumberString);

log cat输出

        02-14 11:15:30.674: I/System.out(9716): The Data is28-1-2013
02-14 11:15:30.674: I/System.out(9716): The Time is1-12
02-14 11:15:30.674: I/System.out(9716): The Phone Number Is855-214
02-14 11:15:31.774: E/DataBase(9716): opened
02-14 11:15:31.794: I/SqliteDatabaseCpp(9716): sqlite returned: error code = 1, msg = no such table: call_details, db=/data/data/com.example.reminder/databases/Reminder
02-14 11:15:31.824: E/SQLiteDatabase(9716): Error inserting timecall=1-12 datecall=28-1-2013 toaddcall=855-214
02-14 11:15:31.824: E/SQLiteDatabase(9716): android.database.sqlite.SQLiteException: no such table: call_details: , while compiling: INSERT INTO call_details(timecall,datecall,toaddcall) VALUES (?,?,?)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:68)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361)
    02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteStatement.acquireAndLock(SQLiteStatement.java:260)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:112)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at com.example.reminder.OpenHelper.calldetails_insertion(OpenHelper.java:87)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at com.example.reminder.callcomposer$3.onClick(callcomposer.java:134)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.view.View.performClick(View.java:3511)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.view.View$PerformClick.run(View.java:14105)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.os.Handler.handleCallback(Handler.java:605)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.os.Looper.loop(Looper.java:137)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at android.app.ActivityThread.main(ActivityThread.java:4424)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-14 11:15:31.824: E/SQLiteDatabase(9716):     at dalvik.system.NativeStart.main(Native Method)
02-14 11:15:31.824: V/DB(9716): Inset_User OK

1 个答案:

答案 0 :(得分:0)

试试如下:

将创建表查询主键更改为自动增量并插入数据,如下所示:

db.execSQL("CREATE TABLE "+Tablenamecall+" ("+key_idcall+" INTEGER PRIMARY KEY AUTOINCREMENT, "+datecall+" TEXT, "+timecall+" TEXT, "+toaddcall+" TEXT)"
  public void calldetails_insertion(String date11 ,String time11 ,String to11) 
 {
     SQLiteDatabase db=this.getWritableDatabase();
     ContentValues cv=new ContentValues();
      cv.put(datecall, date11);
      cv.put(timecall, time11);
      cv.put(toaddcall, to11);
      db.insert(Tablenamecall, null, cv); <---- check this line and change it.
      db.close();
      Log.v("DB", "Inset_User OK"); 
  }