我尝试在我的应用程序中使用现有的sqlite数据库并将我的数据库文件(我在Windows 7 OS中为db浏览器创建sqlite可移植软件)放在android studio的assets文件夹中,我写了一个像这样的databaseOpenHelper类:
package com.example.javad.dbproject;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
public class myDatabaseHelper extends SQLiteOpenHelper {
private static String DB_PATH = "/data/data/com.example.javad.dbproject/databases/";
private static String DB_NAME = "database1.sqlite";
public static final String TB_USER = "dataSheet";
private SQLiteDatabase myDB;
private final Context context;
//constructor
public myDatabaseHelper(Context context) {
super(context, DB_NAME, null, 1);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
//Handle creation tasks, etc.
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Handle upgrade tasks, etc.
}
@Override
public synchronized void close() {
if(myDB != null)
myDB.close();
super.close();
}
//Check if our database already exists
private boolean checkDataBase() {
SQLiteDatabase tempDB = null;
try {
String myPath = DB_PATH + DB_NAME;
tempDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
} catch (SQLiteException e) {
Log.e("tle99 - check", e.getMessage());
}
if (tempDB != null)
tempDB.close();
return tempDB != null ? true : false;
}
/***
* Copy database from source code assets to device
* @throws IOException
*/
public void copyDataBase() throws IOException{
try {
InputStream myInput = context.getAssets().open(DB_NAME);
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("tle99 - copyDatabase", e.getMessage());
}
}
/***
* Open database
* @throws SQLException
*/
public void openDataBase() throws SQLException{
String myPath = DB_PATH + DB_NAME;
myDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
/***
* Check if the database doesn't exist on device, create new one
* @throws IOException
*/
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
} else {
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
Log.e("tle99 - create", e.getMessage());
}
}
}
public List<String> getAllUsers(){
List<String> listUsers = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
Cursor c;
try {
c = db.rawQuery("SELECT * FROM " + TB_USER , null);
if(c == null) return null;
String name;
c.moveToFirst();
do {
name = c.getString(1);
listUsers.add(name);
} while (c.moveToNext());
c.close();
} catch (Exception e) {
Log.e("tle99", e.getMessage());
}
db.close();
return listUsers;
}
我以这种方式修改了主要活动java文件:
public class MainActivity extends ActionBarActivity {
myDatabaseHelper dbHeplper;
ListView lvUsers;
ListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHeplper = new myDatabaseHelper(getApplicationContext());
try {
dbHeplper.createDataBase();
} catch (IOException e) {
e.printStackTrace();
}
lvUsers = (ListView) findViewById(R.id.lvUsers);
List<String> listUsers = dbHeplper.getAllUsers();
if (listUsers != null) {
adapter = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, android.R.id.text1,
listUsers);
lvUsers.setAdapter(adapter);
}
}
但是当我运行这个应用程序时,列表视图不会从我的数据库返回任何值,并且此消息出现在logcat中:“错误打开跟踪文件:没有这样的文件或目录(2)”和“E / SQLiteLog:(1 )没有这样的表:dataSheet“
有人认为Android工作室无法找到或读取我的数据库文件中的信息,欢迎Any Ideas!
这是运行app后的logcat消息: http://textuploader.com/p9zt
答案 0 :(得分:0)
我有那样的经历。要解决这个问题,
- 检查&#34;数据库&#34;文件夹是否存在或不存在。如果不存在,请创建&#34;数据库&#34;文件夹优先。
- 如果&#34;数据库&#34;文件夹已存在,然后调用&#34; copyDataBase&#34;功能
有时,&#34;数据库&#34;早期Android版本中缺少文件夹。
答案 1 :(得分:-1)
首先,我们需要为数据库文件夹创建一个文件夹然后复制 databaseFile然后它工作正常。
public void copyDataBase() throws IOException{
try {
InputStream myInput = context.getAssets().open(DB_NAME);
File dbFile=new dbFile(DB_PATH);
dbFile.mkdirs();
String outputFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outputFileName);
byte[] buffer = new byte[1024];
int length;
while((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
} catch (Exception e) {
Log.e("tle99 - copyDatabase", e.getMessage());
}
}