未创建Android SQLite表

时间:2014-01-15 05:55:31

标签: android sqlite

我正在尝试创建一个统计表,但出于某种原因,在我第一次调用StatisticsAdapter.insertEntry()之前,它不会创建。之后,我可以删除该代码,重新加载新版本,然后一切正常。既然这不会在现实生活中发挥作用,我想知道你怎么能创建一个空表?

目前if(StatisticsAdapter.getCount(isbn) < 1)存在错误,因为它表示没有此类表格。为什么会这样?

主要代码

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);
    }

}

5 个答案:

答案 0 :(得分:2)

尝试扩展SQLiteOpenHelper

覆盖

@Override
  public void onCreate(SQLiteDatabase database) {
    database.execSQL(DATABASE_CREATE);
  }

在调用getWritableDatabase()getReadableDatabase()之一之前,实际上并未创建或打​​开数据库。

答案 1 :(得分:2)

您是否尝试在运行代码之前卸载应用程序?如果您的代码的先前版本创建了db而没有统计表,则必须增加数据库版本(DatabaseHelper.DATABASE_VERSION)以触发onUpgrade调用,这将创建表。

您可以尝试的第二件事是将表名称提取为常量并使用它代替硬编码字符串。也许你的某个地方有错字?

我还建议从设备中提取数据库并使用命令行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

答案 2 :(得分:1)

创建班级DatabaseHandler extends SQLiteOpenHelper

并覆盖如下:

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
    String CREATE_CONTACTS_TABLE = Crate table query;
    db.execSQL(CREATE_CONTACTS_TABLE);
}

现在,您希望从SQLiteDatabase db = cintext.getWritableDatabase();调用Activity来访问数据库表。

答案 3 :(得分:0)

当设备上的应用程序版本较低且您更改数据库(添加表,创建新数据库,...)并安装新版本时,已发生此问题,现在应用程序尝试创建新数据库但不能在数据库上创建和更改修改。

解决方案是:

改变DATABASE_VERSION

应用程序被迫创建新的数据库

好运

答案 4 :(得分:0)

只需更新dbHelper类中的DATABASE_VERSION即可。