Error inserting into sqlite database
I get error inserting into sqlite databse I dont know wats up coz the other tables have the same configuratio and they seem to be working fine except for this one:
package com.messageHider;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;
public class dbConnection extends SQLiteOpenHelper{
private static final int DB_VER开发者_Go百科SION=1;
private static final String DB_NAME="smshider";
//sms table
public static final String TABLE_SMS="sms";
public static final String S_ID=BaseColumns._ID;
public static final String MESSAGE="messages";
public static final String SENDER="sender";
public static final String TYPE="type";
public static final String DATE="date";
//This table is used to hide SMS's using the smsReceiver
public static final String TABLE_CONTACTS="contacts";
public static final String C_ID=BaseColumns._ID;
public static final String CONTACT="contact_number";
public static final String CONTACT_NAME="contact_name";
//Hidden Videos table
public static final String TABLE_VIDEOS="videos";
public static final String V_ID=BaseColumns._ID;
public static final String VIDEO_TITLE="video_title";
//Hidden Images table
public static final String TABLE_IMAGES="images";
public static final String I_ID=BaseColumns._ID;
public static final String IMAGE_NAME="image_name";
//Call Log table
public static final String TABLE_CALLOG="mycallog";
public static final String CALL_ID=BaseColumns._ID;
public static final String CALL_NAME="call_name";
public static final String CALL_NUMBER="call_number";
public static final String CALL_TIME="call_time";
public static final String CALL_TYPE="call_type";
public dbConnection(Context context) {
super(context, DB_NAME,null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql=String.format("create table %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT,%s TEXT,%s TEXT,%s TEXT)",TABLE_SMS,S_ID,MESSAGE,SENDER,TYPE,DATE);
String sql2=String.format("create table %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT,%s TEXT)",TABLE_CONTACTS,C_ID,CONTACT,CONTACT_NAME);
String sql3=String.format("create table %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT)",TABLE_VIDEOS,V_ID,VIDEO_TITLE);
String sql4=String.format("create table %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT)",TABLE_IMAGES,I_ID,IMAGE_NAME);
String sql5=String.format("create table %s(%s INTEGER PRIMARY KEY AUTOINCREMENT,%s TEXT,%s TEXT,%s TEXT,%s TEXT)",TABLE_CALLOG,CALL_ID,CALL_NAME,CALL_NUMBER,CALL_TYPE,CALL_TIME);
db.execSQL(sql);
db.execSQL(sql2);
db.execSQL(sql3);
db.execSQL(sql4);
db.execSQL(sql5);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
The problem is inserting into the 5th tablei.e mycallog Here is the code for inserting:
String time=cursorCallLog.getString(cursorCallLog.getColumnIndex(CallLog.Calls.DATE));
String name=cursorCallLog.getString(cursorCallLog.getColumnIndex(CallLog.Calls.CACHED_NAME));
String type=cursorCallLog.getString(cursorCallLog.getColumnIndex(CallLog.Calls.TYPE));
Log.d("COUNT", number+" "+type+" "+time+" "+name);
SQLiteDatabase write_db=connection.getWritableDatabase();
ContentValues values=new ContentValues();
Date date=new Date(Long.parseLong(time));
java.text.DateFormat format=android.text.format.DateFormat.getDateFormat(getApplicationContext());
String date_sent=format.format(date);
values.put(number, dbConnection.CALL_NUMBER);
values.put(name, dbConnection.CALL_NAME);
values.put(date_sent, dbConnection.CALL_TIME);
values.put(type, dbConnection.CALL_TYPE);
write_db.insert(dbConnection.TABLE_CALLOG, null, values);
Here is the logcat:
07-05 09:12:10.765: ERROR/Database(4754): Error inserting 3=call_type 7/5/2011=call_time John Kulova=call_name 0704678505=call_number
07-05 09:12:10.765: ERROR/Database(4754): android.database.sqlite.SQLiteException: near "3": syntax error: , while compiling: INSERT INTO mycallog(3, 7/5/2011, John Kulova, 0704678505) VALUES(?, ?, ?, ?);
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:36)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1145)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1536)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1410)
07-05 09:12:10.765: ERROR/Database(4754): at com.messageHider.CallMonitorService$callLogObserver.onChange(CallMonitorService.java:72)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.ContentObserver.dispatchChange(ContentObserver.java:133)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.ContentObserver$Transport.onChange(ContentObserver.java:65)
07-05 09:12:10.765: ERROR/Database(4754): at android.database.IContentObserver$Stub.onTransact(IContentObserver.java:53)
07-05 09:12:10.765: ERROR/Database(4754): at android.os.Binder.execTransact(Binder.java:288)
07-05 09:12:10.765: ERROR/Database(4754): at dalvik.system.NativeStart.run(Native Method)
It seems your values.put()
are backwards (i.e. value/key instead of key/value).
Change below lines :
values.put(number, dbConnection.CALL_NUMBER);
values.put(name, dbConnection.CALL_NAME);
values.put(date_sent, dbConnection.CALL_TIME);
values.put(type, dbConnection.CALL_TYPE);
Like this :
values.put(dbConnection.CALL_NUMBER, number);
values.put(dbConnection.CALL_NAME, name);
values.put(dbConnection.CALL_TIME, date_sent);
values.put(dbConnection.CALL_TYPE, type);
精彩评论