我正在尝试编写一个简单的应用程序来计算我使用SQLite按下按钮的次数。只有一个表有两列。我有如下实现的SQLiteOpenHelper:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class SqliteHelperInstance extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "app_stats";
public static final String TABLE_NAME = "day_stats";
public static final String COLUMN_DATE = "date";
public static final String COLUMN_CIGCOUNT = "cigcount";
public static final int DATABASE_VERSION = 1;
private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " + COLUMN_DATE + " text "
+ COLUMN_CIGCOUNT + " integer );";
public SqliteHelperInstance(Context context){
super(context,DATABASE_NAME , null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_DB);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS" + TABLE_NAME);
onCreate(db);
}
}
所以我有一个有两列的表首先是“日期”,接下来是一个计数器。 现在使用我的数据库管理器类,我创建了一个插入表格的方法。
public class DbManager {
private SqliteHelperInstance sqlHelper;
private SQLiteDatabase database;
public DbManager(Context context){
sqlHelper = new SqliteHelperInstance(context);
}
public void open() throws SQLException{
database = sqlHelper.getWritableDatabase();
}
public void addEntry(String dateEntry){
DbEntry newEntry = new DbEntry();
newEntry.setDate(dateEntry);
newEntry.setCounter(0);
values.put(SqliteHelperInstance.COLUMN_DATE, dateEntry);
values.put(SqliteHelperInstance.COLUMN_CIGCOUNT, newEntry.getCigNum());
database.insert(SqliteHelperInstance.TABLE_NAME, null, values);
}
}
但是当我运行我的应用程序时,它给出了一个错误,即“cigcount”列不存在。这是catlog消息:
05-08 16:23:15.830: E/SQLiteLog(1076): (1) table day_stats has no column named cigcount
05-08 16:23:15.850: E/SQLiteDatabase(1076): Error inserting cigcount=0 date=2014-05-08
05-08 16:23:15.850: E/SQLiteDatabase(1076): android.database.sqlite.SQLiteException: table day_stats has no column named cigcount (code 1): , while compiling: INSERT INTO day_stats(cigcount,date) VALUES (?,?)
任何人都可以帮我解决这个问题吗?
答案 0 :(得分:4)
您的列规范之间缺少逗号,
:
private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " + COLUMN_DATE + " text "
+ COLUMN_CIGCOUNT + " integer );";
应该是
private static final String CREATE_DB = "create table " + TABLE_NAME + " ( " + COLUMN_DATE + " text, "
+ COLUMN_CIGCOUNT + " integer );";
修复后,卸载您的应用,以便删除旧的数据库文件。 (您的onUpgrade()
中也存在语法错误,单独递增版本无法更新架构。)