我实际上打算利用我方位的Browser在我的roid方案中建立了数据库。 我通过一些文章读一读,所有发言者都说,使用道路/数据/数据/数据/结果。 我感到困惑不解,我无法在我的档案中找到问题。 我没有数据夹,或者我是否自行制作?
我将记录放在记录中,以便跟踪方案自动终止的情况,并在数据库开放后终止,并声称我的数据库“有食品表”。 我检查了这一数据,它只是作为我数据库唯一表格的输出和roid数据。
My question is Why my database only have 1 table : android_metadata table only but no Food table? What Steps I have missed out?
This is my database info 2 tables Table 1 : Food (_id,name, price) Table 2 : android_metadata
我实际上遵循这一联系::http://www.reign Design.com/blog/using-your-own-sqlite-database-in-android-applications/
这是我处理数据库的守则。
package com.restaurant.sesame;
public class Restaurant {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_PRICE = "price";
private static final String DATABASE_NAME ="Restaurantdb";
private static final String DATABASE_TABLE ="Food";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_PATH ="/data/data/com.restaurant.sesame/databases/";
private static SQLiteDatabase ourDatabase;
private static Context ourContext = null;
private DBHelper ourHelper;
private static class DBHelper extends SQLiteOpenHelper{
/**
* Constructor
* Takes and keeps a reference of the passed context in order to access to the application assets and resources.
* @param context
*/
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
/**
* Creates a empty database on the system and rewrites it with your own database.
* */
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and empty database will be created into the default system path
//of your application so we are gonna be able to overwrite that database with our database.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* Check if the database already exist to avoid re-copying the file each time you open the application.
* @return true if it exists, false if it doesn t
*/
private boolean checkDataBase(){
SQLiteDatabase checkDB = null;
try{
String myPath = DATABASE_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does t exist yet.
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
/**
* Copies your database from your local assets-folder to the just created empty database in the
* system folder, from where it can be accessed and handled.
* This is done by transfering bytestream.
* */
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = ourContext.getAssets().open(DATABASE_NAME);
// Path to the just created empty db
String outFileName = DATABASE_PATH + DATABASE_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DATABASE_PATH + DATABASE_NAME;
ourDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(ourDatabase != null)
ourDatabase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// Add your public helper methods to access and get content from the database.
// You could return cursors by doing "return ourDatabase.query(....)" so it d be easy
// to you to create adapters for your views.
}
public Restaurant(Context c)
{
ourContext = c;
}
public Restaurant open() throws SQLException{
ourHelper = new DBHelper(ourContext);
try {
ourHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
ourHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
return this;
}
public void close()
{
ourHelper.close();
}
public String getData()
{
Log.w("my app", "IN getData TOP!!!!");
String [] columns = new String [] {KEY_ROWID,KEY_NAME,KEY_PRICE};
Log.w("my app", "DDDDDDDDD");
/* to test what table in my database
Cursor c1 = ourDatabase.rawQuery("SELECT name FROM sqlite_master WHERE type = table " , null);
String result1="";
int i =0;
if(c1!= null){
c1.moveToFirst();
result1 =result1 + c1.getString(i);
Log.w("my app", result1);
i++;
}
Log.w("my app", "AAAAAAAAAA");
return result1;
*/
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result ="";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iPrice = c.getColumnIndex(KEY_PRICE);
Log.w("my app", "IN getData MIDDLE!!!!");
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
result = result + c.getString(iRow) +" " +
c.getString(iName) + " " +
c.getString(iPrice) + "
";
}
Log.w("my app", "IN getData END!!!!");
return result;
}
}