在我的应用程序活动中运行良好,但没有创建数据库,logcat 中也没有错误。主要活动类:
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
我正在为模拟器使用虚拟盒,在 DDMS 中我什至看不到数据库已创建。
任何人都可以在这方面帮助我。
在此先感谢西瓦