2017-02-19 104 views
-2

我使用“DB Browser for SQLite”创建了我的文件。请检查我的数据库文件,如果它是好的: https://www.file-upload.net/download-12324299/neueAB.db.htmlAndroid:SQLITE数据库文件已加密或不是数据库

我用这个文档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,(OS error - 2:No such file or directory))“

请帮助我。

+2

'DB_PATH =“/ data/data /”+“com.example.avraham.databases”+“/ databases /”;'不要使用硬编码路径。 –

+0

我应该使用什么? – MartyMc

+0

那篇博文是8岁的......试试这个https://github.com/jgilfelt/android-sqlite-asset-helper –

回答

0

尝试更换此

public final static String DATABASE_NAME = "neueAB.db"; 

public final static String DATABASE_NAME = "neueAB"; 
+1

我认为在检测文件扩展名的源代码中有条件检查。在任何一种情况下,这应该都不重要......文件名仍然足够一致以便打开 –

+0

@ cricket_007是的,我知道添加扩展名和不添加它们应该没有区别,但是我记得我看到了这样的错误,并且解决方案是删除扩展名! –

+0

我改变了它,但错误没有改变,除了现在是“没有这样的表:student_table”。 我真的认为问题出在数据库文件中。但是哪里? – MartyMc

0

答案在这里: buffer.append( “ID:” + res.getString(0)+ “\ n”) ;

它应该是: buffer.append(“_ id:”+ res.getString(0)+“\ n”);

相关问题