我已经编写了这段代码来从sdcard访问数据库...但是我收到错误的没有找到这样的表..plz帮我解决了这个问题。
我的代码:
package sai.electricals.photo.meter.verification.system;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Environment;
import android.util.Log;
class DataBaseHelper extends SQLiteOpenHelper {
private static String DB_NAME ="master.sqlite";// Database name
private static String TAG = "DataBaseHelper"; // Tag just for the LogCat window
private static String DB_PATH = "/sdcard/master.sqlite";
private SQLiteDatabase mDataBase;
private final Context mContext;
DataBaseHelper(final Context context) {
super(context, Environment.getExternalStorageDirectory()
+ File.separator + "/DataBase/" + File.separator
+ DB_NAME, null, 1);
this.mContext = context;
Log.e("ERROR HERE", "1");
}
public void createDataBase() throws IOException
{
//If database not exists copy it from the assets
boolean mDataBaseExist = checkDataBase();
if(!mDataBaseExist)
{
this.getReadableDatabase();
this.close();
// try
//{
//Copy the database from assests
copyDataBase();
Log.e(TAG, "createDatabase database created");
// }
//catch (IOException mIOException)
//{
// throw new Error("ErrorCopyingDataBase");
//}
}
}
//Check that the database exists here: mnt/sdcard
private boolean checkDataBase()
{
File dbFile = new File(DB_PATH);
Log.v("dbFile", dbFile + " "+ dbFile.exists());
return dbFile.exists();
}
//Copy the database from SDCARD
private void copyDataBase() throws IOException
{
InputStream mInput;
mInput = new FileInputStream(DB_NAME);
File directory = new File(DB_PATH);
// String outFileName = DB_PATH;
if (!directory.exists()) {
directory.mkdirs();
}
OutputStream mOutput = new FileOutputStream(directory
.getPath() );
byte[] mBuffer = new byte[1024];
int mLength;
while ((mLength = mInput.read(mBuffer))>0)
{
mOutput.write(mBuffer, 0, mLength);
}
mOutput.flush();
mOutput.close();
mInput.close();
Log.e("HELLO", "I M IN COPYDATABSE");
}
//Open the database, so we can query it
public boolean openDataBase() throws SQLException
{
String mPath = DB_PATH;
Log.v("mPath", mPath);
mDataBase = SQLiteDatabase.openOrCreateDatabase(mPath,null);
//mDataBase = SQLiteDatabase.openDatabase(mPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS);
return mDataBase != null;
}
@Override
public synchronized void close()
{
if(mDataBase != null)
mDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase arg) {
//arg.execSQL(createCredentias);
//Log.e("secondtable", "credentials created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:0)
Environment.getExternalStorageDirectory()
+ File.separator + "/DataBase/" + File.separator
+ DB_NAME
和/sdcard/master.sqlite
不相同,会有所不同。此外,getExernalStorageDirectory()
将在没有外部存储器的设备上返回null(参见HTC One,非常受欢迎)。如果您已经在使用它,我不明白为什么要添加分隔符。
此外,我不确定您是否真的可以在/data/data/{packagename}/databases
以外的其他地方使用数据库。更不用说它是否是一个好的设计。
我建议放入shell并检查是否在上面的目录中创建了文件。
编辑:如果要使用外部数据库,只需将其复制到apps文件夹中的database
目录即可。然后通过将文件名传递给DatabaseHelper的构造函数来打开它。
答案 1 :(得分:0)
像这样改变你的构造函数,
public DataBaseHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
,并在班级开放之下宣布这些;
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "yourDatabaseName";