創造了我新的SQL.I SQLite表有一個名爲emergency_no我使用SQLite管理器創建數據庫。這是一個簡單的數據庫,只有一個名爲「emergency_no」的表。該數據庫名爲emergency_no.sqlite是在我的資產文件夾,但是當我在模擬器上運行它,它表明:沒有發現儘管.sqlite文件得到了在設備
android.sqlite.SQLiteException:no such table :emergency_no:
雖然我emergency_no.sqlite和emergency_no.journel在/data/data/com.citymasetro/database/
我dbhelper代碼如下
package com.citymaestro;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class Dbmanager1 {
public static final String KEY_ROWID="_id";
public static final String policeno="police_no";
public static final String fireno="fire_no";
public static final String ambulanceno="ambulance_no";
public static final String womenno="women_cell";
public static final String DATABASE_NAME="emergency_no";
public static final String DATABASE_TABLE="emergency_no";
public static final int DATABASE_VERSION=1;
private static String DB_PATH = "/data/data/com.citymaestro/databases/";
public Dbhelper ourhelper;
public static Context ourcontext;
public SQLiteDatabase ourdatabase;
private static class Dbhelper extends SQLiteOpenHelper{
private boolean mCreateDatabase = false;
private boolean mUpgradeDatabase = false;
public Dbhelper(Context context) {
super(context, DATABASE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
public void initializeDatabase(String path) {
// DATABASE_PATH = path;
getWritableDatabase();
if(mUpgradeDatabase) {
ourcontext.deleteDatabase(DATABASE_NAME);
}
if(mCreateDatabase || mUpgradeDatabase) {
try {
createDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
public void createDataBase() throws IOException
{
boolean dbExist = checkDataBase();
if(dbExist)
{
//do nothing - database already exist
}else{
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase()
{
SQLiteDatabase checkDB = null;
try
{
String myPath = DB_PATH + DATABASE_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.CREATE_IF_NECESSARY);
}catch(SQLiteException e)
{
//database does't exist yet.
}
if(checkDB != null)
{
checkDB.close();
}
return checkDB != null ? true : false;
}
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 = DB_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();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
/* try {
createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("GAG","oncreatem");
}
*/
mCreateDatabase = true;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
mUpgradeDatabase = true;
}
}
public Dbmanager1(Context c)
{
ourcontext=c;
//Dbhelper dbh=new Dbhelper(ourcontext);
//Log.d("gag","constru");
}
public Dbmanager1 open(){
ourhelper=new Dbhelper(ourcontext);
ourdatabase=ourhelper.getWritableDatabase();
return this;
}
public void close(){
ourhelper.close();
}
public String getdata() {
// TODO Auto-generated method stub
String[] columns= new String[]{ KEY_ROWID,policeno};
Cursor c =ourdatabase.query(DATABASE_TABLE, columns, null, null,null, null, null);
String result="";
int irow=c.getColumnIndex(KEY_ROWID);
int irow2=c.getColumnIndex(policeno);
for(c.moveToFirst(); !c.moveToLast();c.moveToNext()){
result=result + c.getString(irow) + "" +c.getString(irow2) + "\n";
}
return result;
}
}
我從我的searchscreen.java調用這個
String str;
db.open();
// Log.d("TAG","before str");
try {
str=db.getdata();
// Log.d("TAG",str);
db.open();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("TAG",e.toString());
}
searchscreen sc;
//sc= db.getdata(i, k);
db.close();
我不能發現其中的錯誤。在此先感謝
你檢查表甚至存在????,因爲你不是在代碼中創建它。 – Daniel 2013-04-07 07:46:38
檢查答案正確的,如果沒有幫到您: – Daniel 2013-04-07 07:49:41
你檢查'/ databases'文件夾?有'emergency_no'和'emergency_no.sqlite'文件。另外,確保android數據庫也有'android_metadata'表。 – 2013-04-07 11:47:32