我希望从现有数据库中创建一个ListView
(我见过的所有教程都在代码中创建了一个数据库)
我有一个名为Favorites的内部SQLite数据库,它只包含用户最喜欢的体育列表,如何将代码链接到该数据库,而不是每次创建一个新数据并将其链接到Listview
?
非常感谢
答案 0 :(得分:1)
假设您已在SQLite Browser
或任何其他SQLiteGUI Tool
...
在SQLHELPER
中创建Your package
班级。
只需复制粘贴下面的一个
只需在行
中添加您的详细信息即可 private static String DB_PATH="data/data/com.AZone.egba/databases/";
//replace it(com.AZone.egba) with your package name
//Create a folder named "Database" inside assets folder
private static final String DATABASE_NAME="abc.db"; // your database name here
课程的其余部分将提醒相同的
package com.AZone.egba.SqlHelper;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.R.string;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.database.SQLException;
import android.media.JetPlayer.OnJetEventListener;
import android.util.Log;
import android.widget.Toast;
public class SQLHelper extends SQLiteOpenHelper {
private static String DB_PATH="data/data/com.AZone.egba/databases/";
//replace it(com.AZone.egba) with your package name
//Create a folder named "Database" inside assets folder
private static final String DATABASE_NAME="abc.db"; // your database name here
private static final String DATABASE_PATH_ASSETS="Database"+File.separator;
private static final int SCHEMA_VERSION=1;
public SQLiteDatabase myDataBase;
private final Context myContext;
public SQLHelper(Context context) {
super(context, DATABASE_NAME,null,SCHEMA_VERSION);
this.myContext=context;
// TODO Auto-generated constructor stub
}
public void createDataBase() throws IOException{
boolean dbExist=checkDatabase();
if(dbExist){
}
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+DATABASE_NAME;
checkDB=SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
catch (SQLiteException e) {
// TODO: handle exception
}
if(checkDB!=null){
checkDB.close();
}
return checkDB !=null?true:false;
}
private void copyDataBase() throws IOException{
InputStream myInput= myContext.getAssets().open(DATABASE_PATH_ASSETS+DATABASE_NAME);
String outFileName=DB_PATH+DATABASE_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 {
String mypath= DB_PATH+DATABASE_NAME;
myDataBase=SQLiteDatabase.openDatabase(mypath, null, SQLiteDatabase.OPEN_READWRITE);
}
@Override
public synchronized void close() {
if(myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
/////////////////////////////////////////CUSTOM FUNCTIONS/////////////////////////////////////////////
public Cursor GetFavouritesList(){
try
{
return(getReadableDatabase().rawQuery("SELECT _id,Name,Code FROM tblfavourites",null));
}
catch(SQLiteException e)
{
Log.e("tblfavourites", e.toString());
}
return null;
}
}
为了使它工作,你必须将数据库保存在assets文件夹中(这里我在文件夹Database中进一步创建了一步)
在onCreate
SQLHelper helper=null;
然后在您的活动的onCreate()中调用以下方法
public void initialazeDatabase()
{
helper=new SQLHelper(this);
try{
helper.createDataBase();
}catch (IOException ioe) {
throw new Error("Unable To Create Database");
}
try{
helper.openDataBase();
}catch (SQLException sqle) {
throw sqle;
}
}
呼叫
在onDestroy
活动中
helper.close();
现在,您可以访问SQLHelper中定义的方法以从数据库中获取值
在任何地方使用helper.GetFavouritesList()
helper.GetFavouritesList()
将返回Cursor ....
使用Adapter
将游标数据绑定到List view
..(网上提供了各种教程如何将listview
与数据库绑定)
答案 1 :(得分:0)
您不需要在每次运行时都创建数据库。 SQLiteOpenHelper类只创建一次数据库。因此,只需创建一个DatabaseAdapter来查询数据库。
编辑:
创建一个类DatabaseAdapter,如下所示:
public class DatabaseAdapter {
protected SQLiteDatabase database;
public DatabaseAdapter(Context context) {
MyDatabaseHelper databaseHelper = new MyDatabaseHelper(context, "my_db");
database = databaseHelper.getWritableDatabase();
}
protected class MyDatabaseHelper extends SQLiteOpenHelper {
public MyDatabaseHelper(Context context, String name) {
super(context, name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DATABASE_QUERY);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
// Your methods that works with database comes here
}
MyDatabaseHelper类的onCreate方法仅在第一次创建数据库时被调用。
答案 2 :(得分:0)