我使用“DB Browser for SQLite”创建了我的文件。请检查我的数据库文件是否正常: https://www.file-upload.net/download-12324299/neueAB.db.html
我使用此文档https://blog.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/将外部数据库加载到我的Android应用程序中。但经过5个小时的工作,我终于放弃了自己找到问题的方法。这是DataBaseHelper类:
package com.example.avraham.databases;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class DataBaseHelper extends SQLiteOpenHelper {
//Attributes#######
public final static String DATABASE_NAME = "neueAB.db";
public String DB_PATH;
private Context cont;
SQLiteDatabase db;
//Constructor#######
public DataBaseHelper(Context context) {
super(context, DATABASE_NAME, null , 1);
cont = context;
}
//Methods#######
public void createDatabase() throws IOException{
DB_PATH = "/data/data/" + "com.example.avraham.databases" + "/databases/";
boolean dbExist = checkDataBase();
if(dbExist != true){
try{
getReadableDatabase(); //create database whis is empty. There we will copy the entries of the assets database.
copyDataBase();
Toast.makeText(cont, "Copied succ!", Toast.LENGTH_SHORT).show();
}
catch (Exception e){
Toast.makeText(cont, "Error:" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(cont, "Database already exists", Toast.LENGTH_SHORT).show();
}
}
public boolean checkDataBase() {
File dbFile = new File(DB_PATH + DATABASE_NAME);
return dbFile.exists();
}
public void copyDataBase() {
try {
//Open your local db as the input stream
InputStream myInput = cont.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();
Toast.makeText(cont, "Copy erfolgreich!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
MainActivity.showMessage("Error copyDataBase()", e.getMessage(),cont);
}
}
public Cursor getAllData(){
try {
//db = this.getWritableDatabase(); //Create Database
String myPath = DB_PATH+DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Cursor res = db.rawQuery("SELECT * FROM student_table", null);
return res;
}
catch (Exception e){
MainActivity.showMessage("Error getAllData()", e.getMessage(),cont);
return null;
}
}
@Override
public synchronized void close() {
if(db != null)
db.close();
super.close();
}
public boolean openDataBase(){
try{
String myPath = DB_PATH+DATABASE_NAME;
db = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
return db != null;
}
catch(Exception e){
MainActivity.showMessage("Error openDataBase()", e.getMessage(),cont);
return false;
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
@Override
public void onCreate(SQLiteDatabase db) {
}
}
这是活动文件:
package com.example.avraham.databases;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
DataBaseHelper myHelper;
EditText txtName, txtSurname, txtAge, txtId;
Button btnShow, btnAdd, btnUpdate, btnDelete;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeVariables();;
myHelper = new DataBaseHelper(this);
/* ######################################################*/
btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnShow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
myHelper.createDatabase();
} catch (IOException e) {
showMessage("Error", e.getMessage(),MainActivity.this);
}
myHelper.openDataBase();
showAll();
}
});
btnUpdate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
}
public void showAll(){
try{
Cursor res = myHelper.getAllData();
if(res.getColumnCount() == 0){
showMessage("Info", "No Data available",MainActivity.this);
}
else{
StringBuffer buffer = new StringBuffer();
while(res.moveToNext()){
buffer.append("id: " + res.getString(0) + "\n");
buffer.append("Name: " + res.getString(1) + "\n");
buffer.append("Surname: " + res.getString(2) + "\n");
buffer.append("Age: " + res.getString(3) + "\n\n");
}
showMessage("Data:", buffer.toString(),MainActivity.this);
}
}
catch (Exception e){
showMessage("Error showAll():",e.getMessage(),MainActivity.this);
}
}
/*############################################################################################*/
/*############################################################################################*/
/*############################################################################################*/
public static void showMessage(String Title, String Message, Context c){
AlertDialog.Builder dlg = new AlertDialog.Builder(c);
dlg.setTitle(Title);
dlg.setMessage(Message);
dlg.setCancelable(true);
dlg.setPositiveButton("OK", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
}
});
dlg.create().show();
}
public void initializeVariables(){
txtName = (EditText) findViewById(R.id.editTxt_name);
txtSurname = (EditText) findViewById(R.id.editTxt_surname);
txtAge = (EditText) findViewById(R.id.editTxt_age);
btnAdd = (Button) findViewById(R.id.btn_add);
btnShow = (Button) findViewById(R.id.btn_show);
btnUpdate = (Button) findViewById(R.id.btn_update);
txtId = (EditText) findViewById(R.id.txt_id);
btnDelete = (Button) findViewById(R.id.btn_delete);
}
}
我在getAllData()函数中遇到错误:
“文件已加密或不是数据库(Sqlite代码26):,同时编译:SELECT * FROM student_table,(操作系统错误 - 2:没有此类文件或目录))”
请帮帮我。
答案 0 :(得分:0)
尝试替换此
public final static String DATABASE_NAME = "neueAB.db";
与
public final static String DATABASE_NAME = "neueAB";
答案 1 :(得分:0)
答案在这里: buffer.append(" id:" + res.getString(0)+" \ n");
它应该是: buffer.append(" _id:" + res.getString(0)+" \ n");