仍然无法创建该表。问题是什么?
这是我的数据库类
public class ProgramDbAdapter{
public static final String KEY_ROWID = "_id";
public static final String KEY_PROGRAM_TITLE = "ProgramTitle";
public static final String KEY_PROGRAM_DATE = "ProgramDate";
public static final String KEY_PROGRAM_TIME = "ProgramTime";
public static final String KEY_PROGRAM_CHANNEL = "ProgramChannel";
private static final String DATABASE_CREATE =
"CREATE TABLE " + DATABASE_TABLE + " (" + "_id integer primary key autoincrement, "
+ "ProgramTitle text, " + "ProgramDate varchar(20), "
+ "ProgramTime varchar(20), " + "ProgramChannel text)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL(DATABASE_UPGRADE);
onCreate(db);
}
}
public ProgramDbAdapter open() throws SQLException{
mDbHelper = new DatabaseHelper(mcontext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public long CreateData(String ProgramTitle, String ProgramDate, String ProgramTime, String ProgramChannel){
ContentValues initialvalues = new ContentValues();
initialvalues.put(KEY_PROGRAM_TITLE, ProgramTitle);
initialvalues.put(KEY_PROGRAM_DATE, ProgramDate);
initialvalues.put(KEY_PROGRAM_TIME, ProgramTime);
initialvalues.put(KEY_PROGRAM_CHANNEL, ProgramChannel);
return mDb.insert(DATABASE_TABLE, null, initialvalues);
}
}
这是我的主类,
public class Program extends ExpandableListActivity{
public void onCreate(Bundle savedInstanceState) {
mDbHelper = new ProgramDbAdapter(this);
mDbHelper.open();
mDbHelper.CreateData("a","a","a","a");
}
}
当我调用 open() 和 createdata 函数时,logcat 告诉我没有这样的表:Program
。问题出在哪里?
This is my Database class
public class ProgramDbAdapter{
public static final String KEY_ROWID = "_id";
public static final String KEY_PROGRAM_TITLE = "ProgramTitle";
public static final String KEY_PROGRAM_DATE = "ProgramDate";
public static final String KEY_PROGRAM_TIME = "ProgramTime";
public static final String KEY_PROGRAM_CHANNEL = "ProgramChannel";
private static final String DATABASE_CREATE =
"CREATE TABLE " + DATABASE_TABLE + " (" + "_id integer primary key autoincrement, "
+ "ProgramTitle text, " + "ProgramDate varchar(20), "
+ "ProgramTime varchar(20), " + "ProgramChannel text)";
private static final String DATABASE_UPGRADE = "DROP TABLE IF EXISTS " + DATABASE_TABLE;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL(DATABASE_UPGRADE);
onCreate(db);
}
}
public ProgramDbAdapter open() throws SQLException{
mDbHelper = new DatabaseHelper(mcontext);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public long CreateData(String ProgramTitle, String ProgramDate, String ProgramTime, String ProgramChannel){
ContentValues initialvalues = new ContentValues();
initialvalues.put(KEY_PROGRAM_TITLE, ProgramTitle);
initialvalues.put(KEY_PROGRAM_DATE, ProgramDate);
initialvalues.put(KEY_PROGRAM_TIME, ProgramTime);
initialvalues.put(KEY_PROGRAM_CHANNEL, ProgramChannel);
return mDb.insert(DATABASE_TABLE, null, initialvalues);
}
}
This is my main class
public class Program extends ExpandableListActivity{
public void onCreate(Bundle savedInstanceState) {
mDbHelper = new ProgramDbAdapter(this);
mDbHelper.open();
mDbHelper.CreateData("a","a","a","a");
}
}
when i call open() and createdata function, the logcat tell me no such table:Program
. where is the problem is?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
sqlite 中没有 varchar 类型,请参见此处。使用文本代替。
There is no varchar type in sqlite, see here. Use text instead.
我不知道“DATABASE_TABLE”来自哪里。
另外,您应该使用“rawQuery()”、“query()”方法或“SQLiteStatement”将参数绑定到 SQL 语句中。
最后但并非最不重要的一点是,SQLite 不知道任何“varchar”。在此处检查所有数据类型:链接
I don't see where 'DATABASE_TABLE' is coming from.
Also, you should use the "rawQuery()", "query()"-Methods or a "SQLiteStatement" to bind parameters into your SQL-Statement.
And last but not least, SQLite doesn't know any 'varchar'. Check here for all Data types: Link
varchar 被翻译为文本...
来自弗拉基米尔链接:
问题是你在第一次运行时创建了数据库,并且有不同的创建语句...
从android设置中删除应用程序数据或更改DATABASE_VERSION
varchar is translated to text ...
from vladimir link:
problem is that u created db in first run and there was different Create statment ...
delete app data from android settings or change DATABASE_VERSION