在我的应用程序中, Activity 运行良好,但未创建数据库,logcat 中也没有错误。 主要 Activity 类:
package com.example.testdb;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Database d=new Database(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
数据库类:
package com.example.testdb;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class Database extends SQLiteOpenHelper{
String Tablename = "Table1";
private String Column1 = "RegionID";
private String Column2 = "RegionName";
private String Column3 = "Currency";
SQLiteDatabase db;
public Database(Context context) {
super(context, "Test", null, 2);
this.getWritableDatabase();
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
final String r_Table = "CREATE TABLE " + Tablename + " (" + Column1+ " INTEGER PRIMARY KEY , " + Column2 + " TEXT, " + Column3 + " Text) ";
db.execSQL(r_Table);
ContentValues cv = new ContentValues();
cv.put(Column1, 1);
cv.put(Column2, "India");
cv.put(Column3, "Rupee");
db.insert(r_Table, null, cv);
}
catch(Exception e){
System.out.println(e.getMessage());
}
Cursor c = db.rawQuery("Select * from Table1", null);
if (c != null ) {
if (c.moveToFirst()) {
//Toast.makeText(new MainActivity().getApplicationContext(), c.getCount(), Toast.LENGTH_LONG).show();
System.out.println("Rows are:"+c.getCount());
}
}
c.close();
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
日志:
05-18 12:45:22.042: I/ActivityThread(3270): Switching default density from 160 to 130
05-18 12:47:18.162: I/ActivityThread(3415): Switching default density from 160 to 130
我正在为模拟器使用 Virtual box,在 DDMS 中我什至看不到数据库已创建。
谁能帮我解决这个问题。
提前致谢 湿婆
最佳答案
根据文档 SqliteOpenHelper使用惰性模型创建数据库。这意味着,在没有数据库的情况下,不会在构造函数中调用 onCreate,而是在实际需要数据库时调用,即在第一次调用 getReadableDatabase 或 getWritableDatabase 时。 所以,你的情况是正常的。
将 SqliteOpenHelper 视为数据库连接 的提供者,包含用于处理“如果数据库不存在”和“如果数据库已过时”等情况的附加逻辑。 因此,在您的情况下,不必担心实际创建数据库文件的时间。
Activity 中的数据库使用(如果它是唯一的,使用 db)可能是这样的:
private SqliteOpenHelper connection;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.OnCreate(savedInstanceState);
setContentView(R.layout.activity_main);
connection = new Database(this); //no actual db creation here
//if you want to test db table creation without any further work uncomment following line
//SQLiteDatabase db = connection.getReadableDatabase();
}
public void onBtnAddRecordClick(View view) {
SqliteDatabase db = connection.getWritableDatabase(); //if something not right with
//the db this will be corrected here
ContentValues values;
//... fill in the record to insert
db.insert(MY_TABLE_NAME, null, values);
}
重要说明:如果您打算从多个 Activity 访问您的数据库,请考虑使您的数据库扩展 SqliteDatabaseHelper 成为一个单例或将其包装到一个 ContentProvider 中,因为使用多个连接通常不受欢迎没有明确目的的数据库。
关于android - 数据库不是在android中创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624512/