我正在研究从数据库中插入和删除android SQLite。最初我尝试了一个单独的项目工作正常,但当我在项目中实现相同时,我正在努力它不起作用。需要你的帮助。谢谢。 以下是数据库的代码。
public class DBStaffList {
private static final String KEY_STAFFID = "StaffId";
private static final String KEY_STAFFNAME = "StaffName";
private static final String DATABASE_NAME = "DB";
private static final String DATABASE_TABLE = "StaffList";
private static final int DATABASE_VERSION = 1;
private DBStaffListHelper staffListHelper;
private final Context staffListContext;
private SQLiteDatabase staffListDatabase;
private static class DBStaffListHelper extends SQLiteOpenHelper {
public DBStaffListHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" + KEY_STAFFID
+ " TEXT NOT NULL, " + KEY_STAFFNAME + " TEXT NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public DBStaffList (Context c){
staffListContext = c;
}
public DBStaffList open() throws SQLException{
staffListHelper = new DBStaffListHelper(staffListContext);
staffListDatabase = staffListHelper.getWritableDatabase();
return this;
}
public void close(){
staffListHelper.close();
}
public long createStaffEntry(String staffIdd, String name) {
ContentValues cv = new ContentValues();
cv.put(KEY_STAFFID, staffIdd);
cv.put(KEY_STAFFNAME, name);
return staffListDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getStaffData() {
String[] columns = new String[]{KEY_STAFFID, KEY_STAFFNAME};
Cursor c = staffListDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iStaffId = c.getColumnIndex(KEY_STAFFID);
int iStaffName = c.getColumnIndex(KEY_STAFFNAME);
for(c.moveToFirst(); !c.isAfterLast();c.moveToNext()){
result = result + c.getString(iStaffId) + " " + c.getString(iStaffName) + "\n";
}
return result;
}
public void deleteTable() throws SQLException{
staffListDatabase.execSQL("DELETE FROM " + DATABASE_TABLE);
}
访问其他类的代码:
DBStaffList saveStaffList = new DBStaffList(IncidentsActivity.this);
saveStaffList.open();
saveStaffList.createStaffEntry("101", "Rao");
saveStaffList.close();
Log cat:
02-08 09:26:46.361: E/SpannableStringBuilder(8010): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-08 09:26:46.361: E/SpannableStringBuilder(8010): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
02-08 09:26:46.991: E/SQLiteLog(8010): (1) table StaffList has no column named StaffId
02-08 09:26:47.031: E/SQLiteDatabase(8010): Error inserting StaffId=101 StaffName=Rao
02-08 09:26:47.031: E/SQLiteDatabase(8010): android.database.sqlite.SQLiteException: table StaffList has no column named StaffId (code 1): , while compiling: INSERT INTO StaffList(StaffId,StaffName) VALUES (?,?)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1108)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:681)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:589)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1573)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1445)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at com.dimensions.dimensionsapp.DBStaffList.createStaffEntry(DBStaffList.java:68)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at com.dimensions.dimensionsapp.IncidentsActivity.onClick(IncidentsActivity.java:282)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.view.View.performClick(View.java:4262)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.view.View$PerformClick.run(View.java:17421)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.os.Handler.handleCallback(Handler.java:615)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.os.Handler.dispatchMessage(Handler.java:92)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.os.Looper.loop(Looper.java:137)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at android.app.ActivityThread.main(ActivityThread.java:4944)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at java.lang.reflect.Method.invokeNative(Native Method)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at java.lang.reflect.Method.invoke(Method.java:511)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
02-08 09:26:47.031: E/SQLiteDatabase(8010): at dalvik.system.NativeStart.main(Native Method)
当我在一个单独的项目中做同样的事情时,它的工作非常好。请建议。
答案 0 :(得分:2)
没有这样的表异常,因为......
"CREATE TABLE "+ DATABASE_TABLE + " (" +
KEY_COSTCODE + "TEXT NOT NULL, " +
KEY_COSTCODENAME + "TEXT NOT NULL);"
应该是
"CREATE TABLE "+ DATABASE_TABLE + " (" +
KEY_COSTCODE + " TEXT NOT NULL, " +
KEY_COSTCODENAME + " TEXT NOT NULL);"
key_costcode
和key_codename
之后的空格。
答案 1 :(得分:0)
您的onCreate
有错误(请参阅@ user2450263的回答),因此数据库没有表格。修复错误,然后确保再次调用onCreate es(如果数据库退出,则不会调用onCreate)。要实现这一目标,请将DATABASE_VERSION
更改为2
。