네트워킹

오늘:
1,105
어제:
2,375
전체:
2,786,841

고객센타 : 070-7752-2000
팩스 : 070-7752-2001
휴대폰 : 010-9513-0019
email : voipkorea@yahoo.co.kr

국민은행
(주)제이에스솔루션
047101-04-155519

Flag Counter
■ 무료 : 유선 집전화 휴대폰 ( 한국 미국 중국 카나다) ↔ (국내 해외 여행자 상사 주재원 유학생) / 가입무 무제한무료■

http://v4all123.blogspot.jp/2013/03/sqlite-databases-with-external-db.html


http://mobile-development-tutorial.blogspot.jp/2012/12/android-external-sqlite-database.html



SQLite Databases using External DB


This is tutorial for accessing external DB from assets folder. In android We can create sqlite database by using SQLite Database Browser. Now the question is how to access this database in source code. The answer is very simple. Store the database in assets folder that is placed in project folder.


Then we can access this database in dbhelper class using

  1. context.getAssets().open(DB_NAME);  

After this we should copy this database to our root directory. Why because our SQLiteOpenHelper reads data from root directory only. for this we have to open new empty file in root directory by using. 
  1. private String DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";  
  2. String DB_NAME = "database";  
  3. String outFileName = DB_PATH + DB_NAME;  
  4. OutputStream myOutput = new FileOutputStream(outFileName);  
then we have to copy our data to this empty file by using. 
  1. byte[] buffer = new byte[1024];  
  2. int length;  
  3. while ((length = myInput.read(buffer)) > 0) {  
  4. myOutput.write(buffer, 0, length);  
  5. }  

Now we can access our data from root directory db to our project easily. Sample project to implement external sqlite database access in android. 

DBHelper.java
  1. package com.android.sqlite;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.IOException;  
  6. import java.io.InputStream;  
  7. import java.io.OutputStream;  
  8. import android.content.Context;  
  9. import android.database.Cursor;  
  10. import android.database.sqlite.SQLiteDatabase;  
  11. import android.database.sqlite.SQLiteOpenHelper;  
  12.   
  13. public class DBHelper extends SQLiteOpenHelper {  
  14.   
  15.  private static String DB_NAME = "database";  
  16.  private SQLiteDatabase db;  
  17.  private final Context context;  
  18.  private String DB_PATH;  
  19.   
  20.  public DBHelper(Context context) {  
  21.   super(context, DB_NAME, null1);  
  22.   this.context = context;  
  23.   DB_PATH = "/data/data/" + context.getPackageName() + "/" + "databases/";  
  24.  }  
  25.   
  26.  public void createDataBase() throws IOException {  
  27.   
  28.   boolean dbExist = checkDataBase();  
  29.   if (dbExist) {  
  30.   
  31.   } else {  
  32.    this.getReadableDatabase();  
  33.    try {  
  34.     copyDataBase();  
  35.    } catch (IOException e) {  
  36.     throw new Error("Error copying database");  
  37.    }  
  38.   }  
  39.  }  
  40.   
  41.  private boolean checkDataBase() {  
  42.   File dbFile = new File(DB_PATH + DB_NAME);  
  43.   return dbFile.exists();  
  44.  }  
  45.   
  46.  private void copyDataBase() throws IOException {  
  47.   
  48.   InputStream myInput = context.getAssets().open(DB_NAME);  
  49.   String outFileName = DB_PATH + DB_NAME;  
  50.   OutputStream myOutput = new FileOutputStream(outFileName);  
  51.   byte[] buffer = new byte[1024];  
  52.   int length;  
  53.   while ((length = myInput.read(buffer)) > 0) {  
  54.    myOutput.write(buffer, 0, length);  
  55.   }  
  56.   
  57.   // Close the streams  
  58.   myOutput.flush();  
  59.   myOutput.close();  
  60.   myInput.close();  
  61.   
  62.  }  
  63.   
  64.  public Cursor getData() {  
  65.   String myPath = DB_PATH + DB_NAME;  
  66.   db = SQLiteDatabase.openDatabase(myPath, null,  
  67.     SQLiteDatabase.OPEN_READONLY);  
  68.   Cursor c = db.rawQuery("SELECT * FROM master"null);  
  69.    // Note: Master is the one table in External db. Here we trying to access the records of table from external db.  
  70.   return c;  
  71.  }  
  72.   
  73.  @Override  
  74.  public void onCreate(SQLiteDatabase arg0) {  
  75.   // TODO Auto-generated method stub  
  76.  }  
  77.   
  78.  @Override  
  79.  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {  
  80.   // TODO Auto-generated method stub  
  81.  }  
  82. }  

And 
ExternalDBActivity.java 
  1. package com.android.sqlite;  
  2.   
  3. import java.io.IOException;  
  4.   
  5. import android.app.Activity;  
  6. import android.database.Cursor;  
  7. import android.os.Bundle;  
  8. import android.widget.ListView;  
  9. import android.widget.SimpleCursorAdapter;  
  10.   
  11. public class ExternalDBActivity extends Activity {  
  12.  /** Called when the activity is first created. */  
  13.  DBHelper dbhelper;  
  14.   
  15.  @Override  
  16.  public void onCreate(Bundle savedInstanceState) {  
  17.   super.onCreate(savedInstanceState);  
  18.   setContentView(R.layout.main);  
  19.     
  20.   // if you use siplecursoradapter then you should have _id as one of column name and its values should be integer in your db.  
  21.   // so "_id", "columnName1", "columnName2" are column names from your db.  
  22.   String[] from = new String[] { "_id""columnName1""columnName2" };  
  23.   int[] to = new int[] { R.id.TextView1, R.id.TextView2, R.id.TextView3 };  
  24.   
  25.   dbhelper = new DBHelper(this);  
  26.   try {  
  27.    dbhelper.createDataBase();  
  28.   } catch (IOException e) {  
  29.    // TODO Auto-generated catch block  
  30.    e.printStackTrace();  
  31.   }  
  32.   
  33.   Cursor c = dbhelper.getData();  
  34.     
  35.   SimpleCursorAdapter adapter = new SimpleCursorAdapter(  
  36.     getApplicationContext(), R.layout.list, c, from, to);  
  37.     
  38.    ListView list = (ListView) findViewById(R.id.ListView1);  
  39.     
  40.    list.setAdapter(adapter);  
  41.  }  
  42. }  

main.xml 
  1. <?xml version="1.0" encoding="utf-8"?<  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" <  
  6.   
  7.     <ListView  
  8.         android:id="@+id/ListView1"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="wrap_content" <  
  11.     </ListView<  
  12.   
  13. </LinearLayout<  
list.xml 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:gravity="center" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/TextView1"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_margin="10dp" />  
  12.   
  13.     <TextView  
  14.         android:id="@+id/TextView2"  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:layout_margin="10dp" />  
  18.   
  19.     <TextView  
  20.         android:id="@+id/TextView3"  
  21.         android:layout_width="wrap_content"  
  22.         android:layout_height="wrap_content"  
  23.         android:layout_margin="10dp" />  
  24.   
  25. </LinearLayout>  
Related article How to create and use SQLite database dynamically 

Reduce your work by using SQLiteHelper.jar 

Thank You


조회 수 :
13810
등록일 :
2014.02.14
22:14:25 (*.251.139.148)
엮인글 :
http://webs.co.kr/index.php?document_srl=38764&act=trackback&key=459
게시글 주소 :
http://webs.co.kr/index.php?document_srl=38764
List of Articles
번호 제목 글쓴이 날짜 조회 수
55 Sqlite very detail easy tutorial I recommand this admin 2017-09-09 421
54 mysql 로컬접속 풀고 특정 아이피 접속 허가 허락 가능 하게 설정 하는 방법 admin 2017-09-05 423
53 linux command chmod 리눅스 명령어 가장 쉽게 이해하기 설명 사용자 구룹 타인 권한 admin 2017-09-05 431
52 mariadb CREATE USER CREATE USER statement creates new MariaDB accounts. admin 2017-09-01 434
51 MySQL 데이터 베이스 백업 및 복구 방법 admin 2017-09-01 452
50 Allowing MySQL Root Login from All IP Addresses : admin 2017-08-17 391
49 Installation of MySQL Database Server admin 2017-08-17 417
48 mysql 쿼리 로그 남기기 (실시간) admin 2015-04-15 3378
47 SQL Delete records using subqueries admin 2015-04-03 3366
46 Mysql privilege table GRANT SELECT,INSERT,UPDATE,DELETE ON db.table admin 2015-04-02 3551
45 안드로이드 SQLite 속도 향상! insert Transaction admin 2014-04-07 13773
44 MySQL에 원격 접속 허용 여러가지 아이피 아이피대역으로 admin 2014-04-02 10839
43 10gR2_sles10_install file admin 2014-03-18 8837
42 this is final answer assets sqlite Databases trouble copy External DB, check eclips admin 2014-02-18 9291
41 Browse SQLite data on the Android emulator admin 2014-02-15 10826
40 SQLite Database Browser file admin 2014-02-14 9670
» SQLite Databases using External DB admin 2014-02-14 13810
38 sqlite DB copy admin 2014-02-14 9617
37 Android SQLite Database with Multiple Tables admin 2014-02-13 9113
36 Android Simple Clean Good SQLite Database Tutorial 잘된 설명 admin 2014-02-13 9820
35 android sqlite 사용하기 admin 2014-02-10 13869
34 SQLite 개발가이드 데이터베이스의 성능 admin 2014-02-10 15545
33 android - 다수의 Insert 수행시 속도 향상을 위한 팁 sQlite admin 2014-02-10 10363
32 Oracle Linux 에 Oracle DB 설치하기 admin 2013-12-03 13335
31 PreparedStatement mysql java 깔끔한설명 admin 2013-10-26 16367
30 Connect Excel VBA to a MySQL database file admin 2013-09-05 12379
29 Configuring Oracle ASM disks in Oracle Enterprise Linux admin 2013-04-20 10740
28 OS에따른 Oracle 설치버전 admin 2013-04-08 11847
27 RHEL4 + 10g 설치 _silent mode admin 2013-04-08 11733
26 OLE5 + 11G 설치 _silent mode admin 2013-04-08 14890
25 WHERE 조건절 검색시 서브쿼리는 어떻게? admin 2013-04-01 11420
24 CDR 추출 저장 Inner Join 사용 Sql 문 admin 2013-02-05 11730
23 SUPER OCM 1.8club admin 2012-12-18 11122
22 MySQL Java tutorial admin 2012-09-30 16185
21 Oracle 10g Articles admin 2012-06-24 12892
20 기본 10g 설치의 리눅스 세팅에서 추가 해줘야하는 사항(윈도우) admin 2012-06-24 13988
19 SUSE Linux Enterprise Server 10 (Oracle 10g R2 (10.2.0.1)) file admin 2012-03-09 12905
18 Upgrade Oracle from 10.2.0.1 To 10.2.0.4 (Windows) admin 2012-03-06 18011
17 Upgrade Oracle 10g Release 2 from 10201 to 10204 admin 2012-03-05 25493
16 centos 6.2 oracle 10g 설치 admin 2012-03-05 28502
15 Oracle RHEL4+10G 10.2.0.1 설치 10.2.0.5 패치 admin 2012-03-03 17863
14 Oracle Backup & restore with RMAN 기본 admin 2012-02-12 13148
13 오라클 ACE가 해설하는 Oracle Backup & Recovery admin 2012-02-07 13028
12 Oracle Backup & Restore admin 2012-02-07 17436
11 http://www.hoons.kr/ admin 2011-12-19 13090
10 Java && Excel 연동 JAVA 자바로 엑셀을 핸들링 할 수 있는 방법 admin 2011-12-19 32929
9 (C#) ASP.NET MySQL Check Already Exists Add/Insert Record 프로그래밍 코드 admin 2011-12-19 13378
8 xhost and display admin 2011-12-16 12260
7 Oracle 설치 Centos ,openSUSE,엔터프라이즈등 퍼온글 admin 2011-12-16 14181
6 Oracle 의 ASM 구조 알아보기.. admin 2011-12-16 17650