从位于Assets文件夹SQLite数据库中选择数据 [英] Selecting Data from SQLite Database located in Assets folder
问题描述
我有从表中选择数据的问题。这里是我的code:
I am having problems with selecting the data from the table. Here is my code:
第一:位于资产的TEST.SQL文件
First: The test.sql File located in the Assets
BEGIN TRANSACTION;
CREATE TABLE `Student.db` (
`NAME` TEXT,
`LAST` TEXT,
`WORK` TEXT,
`NATIONALITY` TEXT,
`SALARYINCHAR` TEXT
);
INSERT INTO `Student.db` VALUES ('Besho','Jarrah','Teacher','Jordanian','Five Thousends');
INSERT INTO `Student.db` VALUES ('Khaled','Ahmad','Doctor','Syrian','Six Thousends');
COMMIT;
这是在MyDataBase.Java类我的code:
And this is my Code in the MyDataBase.Java Class:
package com.besho.testtheassets;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.readystatesoftware.sqliteasset.SQLiteAssetHelper;
/**
* Created by Besho on 1/7/2016.
*/
public class MyDatabase extends SQLiteAssetHelper {
private static final String DATABASE_NAME = "test.sql";
private static final int DATABASE_VERSION = 1;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
}
这是我的MainActivity类别code:
And here is my MainActivity class code:
package com.besho.testtheassets;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt= (TextView) findViewById(R.id.txt);
Button btn= (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String TABLE_NAME="Student.db";
MyDatabase myDatabase = new MyDatabase(MainActivity.this);
SQLiteDatabase db = myDatabase.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
Cursor cursor= db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
StringBuffer buffer=new StringBuffer();
while (cursor.moveToNext())
{
buffer.append("Name: " + cursor.getString(0) + "\n");
buffer.append("Last: " + cursor.getString(1) + "\n");
buffer.append("Work " + cursor.getString(2) + "\n");
buffer.append("Nationality: " + cursor.getString(3) + "\n");
buffer.append("SalaryinChar: " + cursor.getString(4) + "\n\n\n");
}
txt.setText(buffer.toString());
}
}
});
}
}
应用程序停止工作时,我preSS中的按钮,选择数据到文本视图。请帮我。
The App stopped working when I press the button to select the Data into the Text View. Please, help me.
推荐答案
首先ü应该能够从资产文件夹来访问数据,以便以检索..
First U should able to access data from the Assets folder in order to retrive ..
因此改变乌尔班MyDatabase的如下:
So change ur MyDatabase class as follows :
public class MyDatabase extends SQLiteOpenHelper{
//The Android's default system path of your application database.
private static String DB_PATH = "/data/data/com.besho.testtheassets/databases/";
private static String DB_NAME = "Student.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
public MyDatabase (Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
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 + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
}
if(checkDB != null){
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException{
InputStream myInput = myContext.getAssets().open(DB_NAME);
String outFileName = DB_PATH + DB_NAME;
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))>0){
myOutput.write(buffer, 0, length);
}
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException{
//Open the database
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
然后ü应该能够通过使用自己的乌尔code读取文件或尝试像这样...
我不敢肯定我会工作。试试这个,告诉我们哪些问题乌尔面:
Then U should able to read the files by using ur own code or try like this ... I m not sure i ll work . try this and tell us what problem ur facing:
Mainactivity.class:
Mainactivity.class:
.......
..........
.........
MyDatabase myDbHelper = new MyDatabase ();
myDbHelper = new MyDatabase (this);
try {
myDbHelper.createDataBase();
} catch (IOException ioe) {
throw new Error("Unable to create database");
}
try {
myDbHelper.openDataBase();
}catch(SQLException sqle){
throw sqle;
}
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MyDatabase myDbHelper = new MyDatabase(MainActivity.this);
SQLiteDatabase db = myDatabase.getReadableDatabase();
String selectQuery = "SELECT * FROM " + TABLE_NAME;
Cursor c= db.rawQuery(selectQuery, null);
while (c.moveToNext()) {
//if (cursor.moveToFirst()) {
StringBuffer buffer=new StringBuffer();
while (cursor.moveToNext())
{
buffer.append("Name: " + c.getString(0) + "\n");
buffer.append("Last: " + c.getString(1) + "\n");
buffer.append("Work " + c.getString(2) + "\n");
buffer.append("Nationality: " + c.getString(3) + "\n");
buffer.append("SalaryinChar: " + c.getString(4) + "\n\n\n");
}
txt.setText(buffer.toString());
}
}
});
.........
这篇关于从位于Assets文件夹SQLite数据库中选择数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!