无法在android中创建数据库

时间:2012-03-25 18:46:19

标签: java android database sqlite create-table

下面的代码是我的databasehandler类,我从教程中得到它。除了那个教程,我在很多论坛中看到了这种方法。但是,即使我有创建表,它似乎也没有创建。我的错误可以帮助我。

public class DatabaseHandler extends SQLiteOpenHelper implements
    DatabaseHandlerInterface {

// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;

// Database Name
private static final String DATABASE_NAME = "AACDroidDB";

// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
private static final String TABLE_PHRASES = "phrase";

// Phrase Table Columns names
private static final String KEY_PHRASE_ID = "id";
private static final String KEY_PHRASE_STARTLETTER = "startLetter";
private static final String KEY_PHRASE_FULLPHRASE = "fullPhrase";
private static final String KEY_PHRASE_USAGECOUNT = "usageCount";
private static final String KEY_PHRASE_ISFAMOUS = "isFamous";
private static final String KEY_PHRASE_CREATEDATE = "createDate";
private static final String KEY_PHRASE_LASTUSAGEDATE = "lastUsageDate";

// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";

public DatabaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_PHRASES_TABLE = "CREATE TABLE " + TABLE_PHRASES + "("
            + KEY_PHRASE_ID + " INTEGER PRIMARY KEY,"
            + KEY_PHRASE_STARTLETTER + " text, " 
            + KEY_PHRASE_FULLPHRASE + " text, " 
            + KEY_PHRASE_USAGECOUNT + " integer, "
            + KEY_PHRASE_ISFAMOUS + " integer, "
            + KEY_PHRASE_CREATEDATE + " text, " + KEY_PHRASE_LASTUSAGEDATE
            + " text" + ");";

    db.execSQL(CREATE_PHRASES_TABLE);
    Log.d(Constants.DB_LOG, "database created");
}

// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // Drop older table if existed
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_PHRASES);

    // Create tables again
    onCreate(db);
}

/**
 * All CRUD(Create, Read, Update, Delete) Operations
 */

// Adding new phrase
public void addPhrase(Phrase phrase) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_PHRASE_STARTLETTER, phrase.getStartletter());
    values.put(KEY_PHRASE_FULLPHRASE, phrase.getFullphrase());
    values.put(KEY_PHRASE_USAGECOUNT, phrase.getUsagecount());
    values.put(KEY_PHRASE_ISFAMOUS, phrase.getIsFamous());
    values.put(KEY_PHRASE_CREATEDATE, phrase.getCreateDate());
    values.put(KEY_PHRASE_LASTUSAGEDATE, phrase.getLastUsageDate());
    //
    db.insert(TABLE_PHRASES, null, values);
    db.close(); // Closing database connection
}

// getting new phrase
public Phrase getPhrase(int idForPhrase) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query(TABLE_PHRASES, new String[] { KEY_PHRASE_ID,
            KEY_PHRASE_STARTLETTER, KEY_PHRASE_FULLPHRASE,
            // KEY_PHRASE_USAGECOUNT,
            KEY_PHRASE_ISFAMOUS,
            // KEY_PHRASE_CREATEDATE,
            KEY_PHRASE_LASTUSAGEDATE }, KEY_PHRASE_ID + "=?",
            new String[] { String.valueOf(idForPhrase) }, null, null, null,
            null);

    if (cursor != null)
        cursor.moveToFirst();

    // int id = Integer.parseInt(cursor.getString(0));
    String startLetter = cursor.getString(1);
    String fullPhrase = cursor.getString(2);
    // int usageCount = Integer.parseInt(cursor.getString(3));
    // int isFamous = Integer.parseInt(cursor.getString(4));
    // String createDate = cursor.getString(5);
    // String lastUsageDate = cursor.getString(6);

    Phrase phrase = new Phrase();
    // phrase.setId(id);
    phrase.setStartletter(startLetter);
    phrase.setFullphrase(fullPhrase);
    // phrase.setUsagecount(usageCount);
    // phrase.setIsFamous(isFamous);
    // phrase.setCreateDate(createDate);
    // phrase.setLastUsageDate(lastUsageDate);

    // return contact
    return phrase;
}

// Updating single contact
public int updatePhrase(Phrase phrase) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(KEY_PHRASE_STARTLETTER, phrase.getStartletter());
    values.put(KEY_PHRASE_FULLPHRASE, phrase.getFullphrase());
    values.put(KEY_PHRASE_USAGECOUNT, phrase.getUsagecount());
    values.put(KEY_PHRASE_ISFAMOUS, phrase.getIsFamous());
    values.put(KEY_PHRASE_CREATEDATE, phrase.getCreateDate());
    values.put(KEY_PHRASE_LASTUSAGEDATE, phrase.getLastUsageDate());

    // updating row
    return db.update(TABLE_PHRASES, values, KEY_PHRASE_ID + " = ?",
            new String[] { String.valueOf(phrase.getId()) });
}

}

2 个答案:

答案 0 :(得分:1)

在获得对数据库的引用之前,不会调用onCreate(...)的{​​{1}}方法。

SQLiteHelper中创建帮助程序的实例,然后从中获取可写数据库,如下所示......

Activity

顺便说一句,如果您打算使用游标,我建议您使用名称“_id”作为数据库ID列。在Android中,DatabaseHandler myHelper = new DatabaseHandler(this); SQLiteDatabase db = myHelper.getWritableDatabase(); 必须有一个专门称为Cursor的列才能正常工作。在创建表时使用该特定名称意味着您不必在SQL查询中对列名称进行“别名”。

答案 1 :(得分:0)

只有在设备上首次创建数据库时才会调用onCreate()方法。尝试从设置清除设备上的本地数据 - >应用程序 - >管理应用程序

您也可以更改数据库版本。