首页 > 代码库 > 读取本地已有的.db数据库
读取本地已有的.db数据库
先在项目中导入已有的xxx.db
下面新建DBManager .java和SQLiteActivity.java
package android.sqlite;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import android.content.Context;import android.database.SQLException;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteException;import android.database.sqlite.SQLiteOpenHelper;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.util.Log;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream; import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.os.Environment;import android.util.Log; public class DBManager { private final int BUFFER_SIZE = 400000; public static final String DB_NAME = "health.db"; //保存的数据库文件名 public static final String PACKAGE_NAME = "android.sqlite"; public static final String DB_PATH = "/data" + Environment.getDataDirectory().getAbsolutePath() + "/" + PACKAGE_NAME; //在手机里存放数据库的位置 private SQLiteDatabase database; private Context context; DBManager(Context context) { this.context = context; } public void openDatabase() { this.database = this.openDatabase(DB_PATH + "/" + DB_NAME); } private SQLiteDatabase openDatabase(String dbfile) { try { if (!(new File(dbfile).exists())) {//判断数据库文件是否存在,若不存在则执行导入,否则直接打开数据库 InputStream is = this.context.getResources().openRawResource( R.raw.health); //欲导入的数据库 FileOutputStream fos = new FileOutputStream(dbfile); byte[] buffer = new byte[BUFFER_SIZE]; int count = 0; while ((count = is.read(buffer)) > 0) { fos.write(buffer, 0, count); } fos.close(); is.close(); } SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbfile, null); return db; } catch (FileNotFoundException e) { Log.e("Database", "File not found"); e.printStackTrace(); } catch (IOException e) { Log.e("Database", "IO exception"); e.printStackTrace(); } return null; } public void closeDatabase() { this.database.close(); }}
package android.sqlite;import android.app.Activity;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.widget.Button;public class SQLiteActivity extends Activity { /** Called when the activity is first created. */ public DBManager dbHelper; private SQLiteDatabase database; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); dbHelper = new DBManager(this); dbHelper.openDatabase(); dbHelper.closeDatabase(); database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null); getdata(); database.close(); } private void getdata() { // TODO Auto-generated method stub Cursor cur = database.rawQuery("SELECT * FROM ctbx", null); if (cur != null) { int NUM_CITY = cur.getCount(); if (cur.moveToFirst()) { do { String id = cur.getString(cur.getColumnIndex("id")); String code = cur.getString(cur.getColumnIndex("code")); String name = cur.getString(cur.getColumnIndex("name")); System.out.println(id+" "+code+" "+name); //额外添加一句,把select到的信息输出到Logcat } while (cur.moveToNext()); } } else { return; } } }
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" ><TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /></LinearLayout>
读取本地已有的.db数据库
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。