getWritableDatabase()不调用onCreate()

时间:2011-09-23 19:36:59

标签: android sqlite

我正在我的Android应用程序中编写数据库,但我的表格没有被创建。我在onCreate方法中添加了断点,并且调用了getWriteableDatabase和getWritableDatabase,但是onCreate不是。

package com.fslade.app;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class DatabaseHelper extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "applicationdata";
    private static final int DATABASE_VERSION = 1;
    public static final String DATABASE_TABLE = "peopleT";

    // Database creation sql statement
    private static final String DATABASE_CREATE = "create table " + DATABASE_TABLE + " (_id integer primary key autoincrement, "
            + "company_name text not null, name text not null, city text not null, vertical text not null, title text not null, email text not null, bio text not null, photo text not null, status text not null);";

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

    // Method is called during creation of the database
    @Override
    public void onCreate(SQLiteDatabase database) {
        database.execSQL(DATABASE_CREATE);
    }

    // Method is called during an upgrade of the database, e.g. if you increase
    // the database version
    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion,
            int newVersion) {
        Log.w(DatabaseHelper.class.getName(),
                "Upgrading database from version " + oldVersion + " to "
                        + newVersion + ", which will destroy all old data");
        database.execSQL("DROP TABLE IF EXISTS todo");
        onCreate(database);
    }
}

在我的数据库助手中,我调用了getWriteableDatabse():

package com.fslade.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

public class ContentStorageHelper {
    public static final String KEY_ID = "_id";
    public static final String KEY_COMPANY_NAME = "company_name";
    public static final String KEY_PERSON_NAME = "name";
    public static final String KEY_CITY = "city";
    public static final String KEY_VERTICAL = "vertical";
    public static final String KEY_TITLE = "title";
    public static final String KEY_EMAIL = "email";
    public static final String KEY_BIO = "bio";
    public static final String KEY_PHOTO = "photo";
    public static final String KEY_STATUS = "status";

    private static final String[] COLUMNS = { KEY_ID, KEY_COMPANY_NAME,
            KEY_PERSON_NAME, KEY_CITY, KEY_VERTICAL, KEY_TITLE, KEY_EMAIL,
            KEY_BIO, KEY_PHOTO, KEY_STATUS };

    private Context context;
    private SQLiteDatabase database;
    private DatabaseHelper dbHelper;

    public ContentStorageHelper(Context context) {
        this.context = context;
    }

    public ContentStorageHelper open() throws SQLException {
        dbHelper = new DatabaseHelper(context);
        database = dbHelper.getWritableDatabase();
        // dbHelper.onCreate(database);
// I added this onCreate() to see if it helped, but when I ran it
// it said that the database had already been created
        return this;
    }

    public void close() {
        dbHelper.close();
    }

/**
 * Create a new person If the person is successfully created return the new
 * rowId for that person, otherwise return a -1 to indicate failure.
 */
public long createPerson(String company_name, String name, String city,
        String vertical, String title, String email, String bio,
        String photo, String status) {
    ContentValues initialValues = createContentValues(company_name, name,
            city, vertical, title, email, bio, photo, status);

    return database.insert(DatabaseHelper.DATABASE_TABLE, null,
            initialValues);
}

/**
 * Return a Cursor over the list of all people in the database
 * 
 * @return Cursor over all notes
 */
public Cursor fetchPeopleByVertical(String vertical) {
    String selection = KEY_VERTICAL+"="+vertical;
        Cursor result = database.query(DatabaseHelper.DATABASE_TABLE, COLUMNS,
                selection, null, null, null, null);
        return result;
    }

    private ContentValues createContentValues(String company_name, String name,
            String city, String vertical, String title, String email,
            String bio, String photo, String status) {
        ContentValues values = new ContentValues();
        values.put(KEY_COMPANY_NAME, company_name);
        values.put(KEY_PERSON_NAME, name);
        values.put(KEY_CITY, city);
        values.put(KEY_VERTICAL, vertical);
        values.put(KEY_TITLE, title);
        values.put(KEY_EMAIL, email);
        values.put(KEY_BIO, bio);
        values.put(KEY_PHOTO, photo);
        values.put(KEY_STATUS, status);
        System.out.print("test: " + status);
        return values;
    }
}

5 个答案:

答案 0 :(得分:18)

创建只发生一次(这就是数据库助手的整体意义)。

您是否检查过您的数据库是否已创建?它位于/data/data/your.package.name/databases/dbname

如果您在Android应用程序设置中清除了应用程序数据,则应再次调用onCreate ...

答案 1 :(得分:10)

这是一个相当愚蠢的实施限制。 :-)确保数据库名称具有.db扩展名。如果没有扩展名,则不会调用onCreate()

答案 2 :(得分:0)

只有当应用程序的持久存储(即SD卡)中不存在数据库时,才会在SQLiteOpenHelper.onCreate()之后(或SQLiteOpenHelper.getWritableDatabase()之后)回叫

SQLiteOpenHelper.getReadableDatabase()。换句话说,如果代码成功创建了一个存储在持久存储中的数据库,则onCreate()只被调用一次。如果数据库存在于持久存储中,但您正在更改其架构,

使用SQLiteOpenHelper.onUpgrade(),当您按名称打开数据库但使用之前未使用的版本号时,会调用该SQLiteOpenHelper.onCreate()。在其中删除旧的模式对象,然后在其中调用SQLiteOpenHelper.get[Writable,Readable]Database()来创建新模式。如果要在打开数据库时运行代码(onOpen()回调代码),请实现SQLiteOpenHelper.onCreate()。然后调用一个方法来初始化从SQLiteOpenHelper.onOpen()SQLiteDatabase中调用的数据库(或针对您的数据库初始化的代码)。

例如,您可能希望使用SQLiteDatabase.execSQL()的引用,以此来调用SQLiteOpenHelper.onCreate(SQLiteDatabase db) 等数据管理方法。

每个

SQLiteOpenHelper.onOpen(SQLiteDatabase db)

void initDB(SQLiteDatabase db)
{
    this.db = db;
}

调用类似

的内容
SQLiteOpenHelper.onCreate()

您应该处理代码中的其他对象生命周期事件回调处理程序,因为Android多任务处理您的进程。但至少您的数据库处理代码必须处理SQLiteOpenHelper.onOpen()和{{1}}。

答案 3 :(得分:0)

我遇到了同样的问题。我资助2个解决方案

  1. 重新启动模拟器并选中“擦除用户数据”     我的模拟器重启速度很慢......
  2. 更改数据库的版本号,再次调用“OnCreate”,并为我提供了数据库的当前版本。我丢失了存储的信息(没什么大不了的)
  3. 祝你好运

答案 4 :(得分:0)

我遇到了同样的问题。我忘记了SQL_Create字符串中两个sql命令之间的空格,所以我需要的列没有正确实现,但是当我发现并修复String中的错误时,它没有解决问题,因为App已经将该数据库注册为具有该表。所以onCreate从未被调用过。对我来说,最简单的解决方案是更新版本号。这创建了一个新的数据库和表(正确),并修复了应用程序。