未创建Android SQLite表 [英] Android SQLite Table not created
问题描述
我试图创建一个统计表,但是由于某种原因,直到我第一次调用 StatisticsAdapter.insertEntry()
时,它才会创建.之后,我可以删除该代码,重新加载新版本,然后一切正常.既然这在现实生活中行不通,我想知道如何创建一个空表?
I'm trying to create a table of statistics but for some reason, it doesn't create until I call StatisticsAdapter.insertEntry()
for the first time. After that, I can delete that code, reload a new version, and then everything works fine. Since that's not going to work in real life, I was wondering how can you create an empty table?
当前,在 if(StatisticsAdapter.getCount(isbn)< 1)
处出现错误,因为它说没有这样的表.为什么会这样?
Currently there is an error at the if(StatisticsAdapter.getCount(isbn) < 1)
because it says there is no such table. Why is that happening?
主要代码
StatisticsAdapter=new StatisticsAdapter(this);
StatisticsAdapter=StatisticsAdapter.open();
Calendar curDate = Calendar.getInstance();
if(StatisticsAdapter.getCount(isbn) < 1)
StatisticsAdapter.insertEntry(isbn, curDate.get(Calendar.YEAR), (curDate.get(Calendar.MONTH)+1), 1);
else
StatisticsAdapter.increaseCount(isbn);
StatisticsAdapter
public class StatisticsAdapter {
public static final int NAME_COLUMN = 1;
static final String tableName="STATISTICS";
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
public static final String STATISTICS_TABLE_CREATE = "create table "+"STATISTICS"
+ "( " +"ID"+" integer primary key autoincrement,"
+ "ISBN text,"
+ "YEAR text,"
+ "MONTH text,"
+ "COUNT integer); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public StatisticsAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context);
}
public StatisticsAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}
public void insertEntry(String ISBN, int year, int month, int count)
{
ContentValues newValues = new ContentValues();
// Assign values for each row.
newValues.put("ISBN", ISBN);
newValues.put("YEAR", ((Integer)year).toString());
if(month>=10)
newValues.put("MONTH", ((Integer)month).toString());
else
newValues.put("MONTH", "0"+((Integer)month).toString());
newValues.put("COUNT", count);
// Insert the row into your table
db.insert("STATISTICS", null, newValues);
///Toast.makeText(context, "Reminder Is Successfully Saved", Toast.LENGTH_LONG).show();
}
public int getCount(String ISBN)
{
//Query
String query = "select COUNT from STATISTICS where ISBN = ?";
Cursor cursor = db.rawQuery(query, new String[] {ISBN});
if(cursor.getCount()<1) // title Not Exist
{
cursor.close();
return 0;
}
cursor.moveToFirst();
int count = cursor.getInt(cursor.getColumnIndex("COUNT"));
cursor.close();
return count;
}
DataBaseHelper
public class DataBaseHelper extends SQLiteOpenHelper
{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "database.db";
// ============================ End Variables ===========================
public DataBaseHelper(Context context, String name, CursorFactory factory, int version)
{
super(context, name, factory, version);
}
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Called when no database exists in disk and the helper class needs
// to create a new one.
@Override
public void onCreate(SQLiteDatabase _db)
{
_db.execSQL(LoginDataBaseAdapter.USER_TABLE_CREATE);
_db.execSQL(CheckOutDataBaseAdapter.CHECKOUT_TABLE_CREATE);
_db.execSQL(InventoryAdapter.INVENTORY_TABLE_CREATE);
_db.execSQL(StatisticsAdapter.STATISTICS_TABLE_CREATE);
}
// Called when there is a database version mismatch meaning that the version
// of the database on disk needs to be upgraded to the current version.
@Override
public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion)
{
// Log the version upgrade.
Log.w("TaskDBAdapter", "Upgrading from version " +_oldVersion + " to " +_newVersion + ", which will destroy all old data");
// Upgrade the existing database to conform to the new version. Multiple
// previous versions can be handled by comparing _oldVersion and _newVersion
// values.
// on upgrade drop older tables
_db.execSQL("DROP TABLE IF EXISTS " + LoginDataBaseAdapter.USER_TABLE_CREATE);
_db.execSQL("DROP TABLE IF EXISTS " + CheckOutDataBaseAdapter.CHECKOUT_TABLE_CREATE);
_db.execSQL("DROP TABLE IF EXISTS " + InventoryAdapter.INVENTORY_TABLE_CREATE);
_db.execSQL("DROP TABLE IF EXISTS " + StatisticsAdapter.STATISTICS_TABLE_CREATE);
// Create a new one.
onCreate(_db);
}
}
推荐答案
在运行代码之前,您是否尝试过卸载应用程序?如果您的代码的先前版本创建了不带统计信息表的db,则必须增加数据库版本(DatabaseHelper.DATABASE_VERSION)才能触发onUpgrade调用,这将创建该表.
Have you tried uninstalling the app before running the code? If the previous version of your code created db without statistics table, you have to increment your database version (DatabaseHelper.DATABASE_VERSION) to trigger onUpgrade call, which will create the table.
您可以尝试的第二件事是将表名提取为常量,并使用它代替硬编码的字符串.也许您在某处有错字?
The second thing you can try is to extract table name into constant and using it instead hardcoded strings. Maybe you have a typo somewhere?
我还建议从设备中提取数据库,并使用命令行sqlite客户端检查其架构:
I'd also recommend pulling the database from device and checking its schema with command line sqlite client:
adb shell
busybox cp data/data/com.your.package/databases/database.db /sdcard
exit
adb pull /sdcard/database.db
sqlite3 database.db
.schema
这篇关于未创建Android SQLite表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!