本文介绍了不能在android中创建数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的代码是我的databasehandler类,我从一个教程。除了那个教程,我在这么多的论坛中看到了这种方法。但是,即使我有创建表,似乎不是创建。

below code is my databasehandler class i got it from a tutorial. Beside that tutorial i saw this method in so many forums. However, even if i have create table it doesnt seems to be created. What is my wrong can so help me pls.

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

}

推荐答案

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

The onCreate() method is only called when the database is first created on the device. Try clearing local data on the device from Settings -> Applications -> Manage applications

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

You could also change the database version.

这篇关于不能在android中创建数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 21:27