我知道很多同名的问题都存在,但这次是不同的,所以请完整阅读。
我的SQLite代码如下
public class DBHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "iListen";
private static final String TABLE_NAME = "MeetingDetails";
private static final String KEY_MEETING_NAME = "MeetingName";
private static final String KEY_CONFERENCE_DESC = "ConferenceDescription";
private static final String KEY_TIME = "MeetingTime";
private static final String KEY_DURATION = "MeetingDuration";
private static final String KEY_CREATE_TIME = "CreationTime";
private static final String KEY_PRESENTER = "MeetingPresenter";
private static final String KEY_MEETING_KEY= "MeetingId";
private static final String KEY_MEETING_STATUS = "Flag";
public DBHandler(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_MEETING_TABLE = "CREATE TABLE " + TABLE_NAME + "("
+ KEY_MEETING_KEY + " INTEGER PRIMARY KEY," + KEY_MEETING_NAME+ " TEXT," +KEY_CONFERENCE_DESC+ " TEXT,"+ KEY_TIME+ " TEXT,"+KEY_DURATION+ " TEXT,"+KEY_CREATE_TIME+" TEXT,"+
KEY_PRESENTER+" TEXT,"+KEY_MEETING_STATUS+" INTEGER"+")";
db.execSQL(CREATE_MEETING_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS"+TABLE_NAME);
onCreate(sqLiteDatabase);
}
public long addMeeting(MeetingList meetingList){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cValues = new ContentValues();
cValues.put(KEY_MEETING_KEY, meetingList.getId());
cValues.put(KEY_MEETING_NAME, meetingList.getMeetingName());
cValues.put(KEY_CONFERENCE_DESC,meetingList.getConferenceDesc());
cValues.put(KEY_TIME,meetingList.getTime());
cValues.put(KEY_DURATION, meetingList.getDuration());
cValues.put(KEY_CREATE_TIME, meetingList.getCreateTime());
cValues.put(KEY_PRESENTER, meetingList.getPresenter());
cValues.put(KEY_MEETING_STATUS,meetingList.getStatus());
long output = db.insert(DATABASE_NAME,null,cValues);
db.close();
return output;
}
public MeetingList getMeeting(Integer id){
SQLiteDatabase db = this.getReadableDatabase();
// Cursor cr = db.query(TABLE_NAME,new String[]{KEY_MEETING_KEY,KEY_MEETING_NAME, KEY_CONFERENCE_DESC,KEY_TIME,KEY_DURATION,KEY_CREATE_TIME,KEY_PRESENTER,KEY_MEETING_STATUS},KEY_MEETING_KEY+"=?",new String[]
// {String.valueOf(id)},null,null,null,null);
Cursor cr = db.rawQuery("select * from "+TABLE_NAME+" where "+KEY_MEETING_KEY+" = ?",new String[] {id.toString()});
MeetingList list;
if(cr.moveToFirst()) {
// cr.moveToFirst();
list = new MeetingList(Integer.parseInt(cr.getString(0)), cr.getString(1), cr.getString(2), cr.getString(3), cr.getString(4), cr.getString(5), cr.getString(6), Integer.parseInt(cr.getString(7)));
}else{
list = null;
}
return list;
}
}
我尝试在数据库中插入一行,但它始终返回-1
所以当我查看日志时,我看到了这个错误
Error inserting CreationTime=Time = 13-02-2017 9 : 13 MeetingDuration=5 MeetingId=1 Flag=-1 ConferenceDescription=Thus club meeting was going to be held yesterday MeetingTime=Time = 13-02-2017 9 : 13 MeetingName=meeting 2 MeetingPresenter=Randomuser
android.database.sqlite.SQLiteException: no such table: iListen (code 1): , while compiling: INSERT INTO iListen(CreationTime,MeetingDuration,MeetingId,Flag,ConferenceDescription,MeetingTime,MeetingName,MeetingPresenter) VALUES (?,?,?,?,?,?,?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
我用Google搜索并发现了几个帖子,几乎所有帖子都建议我重新安装应用程序,所以我卸载并再次安装它,我收到同样的错误。然后我尝试了一些可能性并得出结论,“创建``声明可能是错误的,它是,我错过了一些逗号,所以我添加了然后我再次得到同样的错误。
任何想法此代码有什么问题?
答案 0 :(得分:3)
你有
db.insert(DATABASE_NAME,null,cValues);
你的第一个参数必须是表名。改为
db.insert(TABLE_NAME,null,cValues);
检查参数@ https://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html#insert(java.lang.String,java.lang.String,android.content.ContentValues)