I tried to use a existing sqlite database in my application and and put my database file (that I created by Db browser for sqlite portable software in windows 7 OS)in assets folder of android studio, I write a databaseOpenHelper class like this :
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;
}
and I modified main activity java file in this way:
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);
}
}
But when I run this app,list view does not return any value from my database and this message appears in logcat:"error opening trace file: No such file or directory (2)" and "E/SQLiteLog﹕ (1) no such table: dataSheet"
It seeems that Android studio can not locate or read information from my database file,Any Ideas welcome!
Aucun commentaire:
Enregistrer un commentaire