我正在我的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;
}
}发布于 2011-09-24 03:44:56
创建只发生一次(这就是整个意义上的数据库帮助器)。
你有没有检查你的数据库是否被创建了?它位于/data/data/your.package.name/databases/dbname。
如果你在安卓应用程序设置中清除了你的应用程序数据,你的onCreate应该会被再次调用...
发布于 2013-08-06 04:45:42
这是一个相当愚蠢的实现限制。:-)确保数据库名称具有.db扩展名。如果onCreate()没有扩展名,则不会调用它。
发布于 2011-10-16 14:13:43
仅当数据库不存在于应用程序的持久存储中时(即,.SD卡)。换句话说,如果代码成功地创建了一个存储在持久存储中的数据库,则只会调用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;
}代码中的回调处理程序还应处理其他对象生命周期事件,如Android多任务进程。但至少您的数据库处理代码必须处理SQLiteOpenHelper.onCreate()和SQLiteOpenHelper.onOpen()。
https://stackoverflow.com/questions/7533964
复制相似问题