import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class Databasehelper extends SQLiteOpenHelper {
public static final String Dname="info.db";
public static final String table_name="exp";
public static final String col1="CATEGORY";
public static final String col2="STATUS";
public static final String col3="AMT";
public static final String col4="DES";
public static final String table2_name="past";
public static final String col12="past_exp";
public static final String CREATE_FIRST_TABLE =
"CREATE TABLE " + table_name + " (" +
col1 + " varchar(25), " +
col2 + " varchar(10), " +
col3 + " float(5,3), " +
col4 + " varchar(255));";
public static final String CREATE_SECOND_TABLE =
"CREATE TABLE " + table2_name + " (" +
col12 + " float(10,2) NOT NULL DEFAULT '0.0');";
public Databasehelper(Context context) {
super(context, Dname, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_FIRST_TABLE);
db.execSQL(CREATE_SECOND_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS "+table_name);
onCreate(db);
db.execSQL("DROP TABLE IF EXISTS "+table2_name);
onCreate(db);
}
public boolean insertData(String category,String status,double amt,
String des) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(col1, category);
cv.put(col2, status);
cv.put(col3, amt);
cv.put(col4, des);
long result=db.insert(table_name, null, cv);
if (result==(-1)) {
return false;
} else {
return true;
}
}
public boolean insertData2(float exp) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(col12, exp);
long result=db.insert(table2_name, null, cv);
if (result==(-1)) {
return false;
} else {
return true;
}
}
public Cursor getData() {
SQLiteDatabase db=this.getWritableDatabase();
Cursor c=db.rawQuery("select * from "+table_name, null);
return c;
}
public Cursor getData2() {
SQLiteDatabase db=this.getWritableDatabase();
Cursor c=db.rawQuery("select * from "+table2_name, null);
return c;
}
}
我需要制作两个表 - exp和过去。我在onCreate(Sqlitdatabase db)
方法中使用exec创建了两个表,但第二个表未创建,应用程序崩溃。什么时候onCreate
被称为完全?我在2个不同的类中创建了2个databasehelper并一个接一个地使用它们。这是我的数据库助手类...如何在上面给出的数据库助手类中使用相同的onCreate
方法制作2个表
我们是否需要2个数据库助手才能创建2个表或1个就足够了?
请尽可能尽快回复。如何在数据库助手类中使用相同的onCreate
方法制作2个表?
logcat错误:
09-12 20:33:19.178: E/AndroidRuntime(21084): Caused by: android.database.sqlite.SQLiteException: no such table: past (code 1): , while compiling: select * from past
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1314)
09-12 20:33:19.178: E/AndroidRuntime(21084): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1253)
答案 0 :(得分:1)
&#34; onCreate究竟何时被调用?&#34;
onCreate(SQLiteDatabase db)方法在应用程序第一次与数据库交互时执行。 DOC.
&#34;我们是否需要2个数据库帮助才能创建2个表或1个就足够了?&#34;
使用1个DatabaseHelper,您可以创建所需数量的表。您甚至可以使用一个DatabaseHelper创建多个数据库。
&#34;如何使用相同的onCreate创建2个表?&#34;
你的onCreate()方法没有问题。
关于logcat错误:
给你logcat错误的方法是什么?您确定在调用该方法时是否创建了数据库?
我制作了一个测试应用,您的代码应该运行。也许您只使用应用程序的&#34;第一次运行&#34; 中的第一个表创建了数据库,之后,您编辑了代码并添加了第二个表和第二个选择,但第二次运行您的应用程序时,将不会创建或更新数据库。如果我对你的应用程序应该运行,如果你在构造函数上更改数据库版本并在创建查询器上进行更改。
您可以阅读文档here。
public static final String CREATE_FIRST_TABLE =
"CREATE TABLE IF NOT EXISTS " + table_name + " (" +
col1 + " varchar(25), " +
col2 + " varchar(10), " +
col3 + " float(5,3), " +
col4 + " varchar(255));";
public static final String CREATE_SECOND_TABLE =
"CREATE TABLE IF NOT EXISTS " + table2_name + " (" +
col12 + " float(10,2) NOT NULL DEFAULT '0.0');";
public Databasehelper(Context context) {
// The last number is the database_version.
// If your app was already instaled and the db created
// and you need to change or create new tables, you just increase the last number.
// (Be aware that you can't downgrade the database version in this method.
// If for some reason you need to dowgrade the db, you will need to override the onDowngrade())
super(context, Dname, null, 1);
}