1 package com.android.ImportDatabase;
2
3 import android.app.Activity;
4 import android.content.Intent;
5 import android.os.Bundle;
6
7 public class RootView extends Activity {
8
9 public DBManager dbHelper;
view sourceprint?01 @Override
02 public void onCreate(Bundle savedInstanceState) {
03
04 super.onCreate(savedInstanceState);
05 setContentView(R.layout.main);
06
07 dbHelper = new DBManager(this);
08 dbHelper.openDatabase();
09 dbHelper.closeDatabase();
10
11 }
12 }
此时在DDMS中可以查看到,外部数据库已经成功导入
在需要使用数据库的类里:
01 package com.android.ImportDatabase;
02
03 import java.util.ArrayList;
04 import android.app.Activity;
05 import android.database.Cursor;
06 import android.database.sqlite.SQLiteDatabase;
07 import android.os.Bundle;
08
09 public class TaxiActivity extends Activity {
10
11 private SQLiteDatabase database;
12 ArrayList CITY;
13
14 @Override
15 public void onCreate(Bundle savedInstanceState) {
16
17 super.onCreate(savedInstanceState);
18 setContentView(R.layout.main);
19
20 database = SQLiteDatabase.openOrCreateDatabase(DBManager.DB_PATH + "/" + DBManager.DB_NAME, null);
21
22 CITY = getCity();
23
24 // do something with CITY
25
26 database.close();
27 }
28
29 private ArrayList getCity() {
30
31 Cursor cur = database.rawQuery("SELECT city.id_city, city.name FROM taxi, city WHERE city.id_city = taxi.id_city GROUP BY city.id_city", null);
32
33 if (cur != null) {
34 int NUM_CITY = cur.getCount();
35 ArrayList taxicity = new ArrayList(NUM_CITY);
36 if (cur.moveToFirst()) {
37 do {
38 String name = cur.getString(cur.getColumnIndex("name"));
39 int id = cur.getInt(cur.getColumnIndex("id_city"));
40 CityClass city = new CityClass("", 0);
41 System.out.println(name); //额外添加一句,把select到的信息输出到Logcat
42 city.city_name = name;
43 city.city_id = id;
44 taxicity.add(city);
45 } while (cur.moveToNext());
46 }
47 return taxicity;
48 } else {
49 return null;
50 }
51 }
52 }
查看输出的结果:
001 01-03 22:32:02.970: INFO/System.out(18231): AGEN
002 01-03 22:32:02.970: INFO/System.out(18231): AJACCIO
003 01-03 22:32:02.970: INFO/System.out(18231): ALBI
004 01-03 22:32:02.980: INFO/System.out(18231): ALEN??ON
005 01-03 22:32:02.980: INFO/System.out(18231): AMIENS
006 01-03 22:32:02.980: INFO/System.out(18231): ANGERS
007 01-03 22:32:02.990: INFO/System.out(18231): ANGOUL??ME
008 01-03 22:32:02.990: INFO/System.out(18231): ANNECY
009 01-03 22:32:03.000: INFO/System.out(18231): ARGENTEUIL
010 01-03 22:32:03.000: INFO/System.out(18231): ARRAS
011 01-03 22:32:03.000: INFO/System.out(18231): AUCH
012 01-03 22:32:03.009: INFO/System.out(18231): AURILLAC
013 01-03 22:32:03.009: INFO/System.out(18231): AUXERRE
014 01-03 22:32:03.009: INFO/System.out(18231): AVIGNON
015 01-03 22:32:03.019: INFO/System.out(18231): BASTIA
016 01-03 22:32:03.019: INFO/System.out(18231): BAYONNE
017 01-03 22:32:03.019: INFO/System.out(18231): BEAUVAIS
018 01-03 22:32:03.019: INFO/System.out(18231): BELFORT
019 01-03 22:32:03.029: INFO/System.out(18231): BESAN??ON
020 01-03 22:32:03.029: INFO/System.out(18231): BLOIS
021 01-03 22:32:03.029: INFO/System.out(18231): BOBIGNY
022 01-03 22:32:03.029: INFO/System.out(18231): BORDEAUX
023 01-03 22:32:03.029: INFO/System.out(18231): BOURG-EN-BRESSE
024 01-03 22:32:03.039: INFO/System.out(18231): BOURGES
025 01-03 22:32:03.039: INFO/System.out(18231): BREST
026 01-03 22:32:03.039: INFO/System.out(18231): CAEN
027 01-03 22:32:03.050: INFO/System.out(18231): CAHORS
028 01-03 22:32:03.050: INFO/System.out(18231): CARCASSONNE
029 01-03 22:32:03.050: INFO/System.out(18231): CASTRES
030 01-03 22:32:03.050: INFO/System.out(18231): CH??LONS-EN-CHAMPAGNE
031 01-03 22:32:03.059: INFO/System.out(18231): CHALON-SUR-SA??NE
032 01-03 22:32:03.059: INFO/System.out(18231): CHAMB??RY
033 01-03 22:32:03.059: INFO/System.out(18231): CHARLEVILLE-M??ZI??RES
034 01-03 22:32:03.069: INFO/System.out(18231): CHARTRES
035 01-03 22:32:03.080: INFO/System.out(18231): CH??TEAUROUX
036 01-03 22:32:03.080: INFO/System.out(18231): CHAUMONT
037 01-03 22:32:03.089: INFO/System.out(18231): CHERBOURG
038 01-03 22:32:03.089: INFO/System.out(18231): COLMAR
039 01-03 22:32:03.089: INFO/System.out(18231): CR??TEIL
040 01-03 22:32:03.089: INFO/System.out(18231): DAX
041 01-03 22:32:03.089: INFO/System.out(18231): DIGNE
042 01-03 22:32:03.099: INFO/System.out(18231): DIJON
043 01-03 22:32:03.099: INFO/System.out(18231): ??PINAL
044 01-03 22:32:03.099: INFO/System.out(18231): ??VREUX
045 01-03 22:32:03.099: INFO/System.out(18231): ??VRY
046 01-03 22:32:03.110: INFO/System.out(18231): FOIX
047 01-03 22:32:03.110: INFO/System.out(18231): GRENOBLE
048 01-03 22:32:03.129: INFO/System.out(18231): GU??RET
049 01-03 22:32:03.129: INFO/System.out(18231): LA ROCHELLE
050 01-03 22:32:03.129: INFO/System.out(18231): LA ROCHE-SUR-YON
051 01-03 22:32:03.129: INFO/System.out(18231): LANGON
052 01-03 22:32:03.129: INFO/System.out(18231): LAON
053 01-03 22:32:03.139: INFO/System.out(18231): LAVAL
054 01-03 22:32:03.139: INFO/System.out(18231): LE HAVRE
055 01-03 22:32:03.139: INFO/System.out(18231): LE MANS
056 01-03 22:32:03.139: INFO/System.out(18231): LE PUY-EN-VELAY
057 01-03 22:32:03.159: INFO/System.out(18231): LIBOURNE
058 01-03 22:32:03.159: INFO/System.out(18231): LILLE
059 01-03 22:32:03.169: INFO/System.out(18231): LIMOGES
060 01-03 22:32:03.169: INFO/System.out(18231): LISIEUX
061 01-03 22:32:03.179: INFO/System.out(18231): LONS-LE-SAUNIER
062 01-03 22:32:03.179: INFO/System.out(18231): LORIENT
063 01-03 22:32:03.179: INFO/System.out(18231): LYON
064 01-03 22:32:03.179: INFO/System.out(18231): M??CON
065 01-03 22:32:03.189: INFO/System.out(18231): MARSEILLE
066 01-03 22:32:03.189: INFO/System.out(18231): MEAUX
067 01-03 22:32:03.189: INFO/System.out(18231): MELUN
068 01-03 22:32:03.189: INFO/System.out(18231): MENDE
069 01-03 22:32:03.189: INFO/System.out(18231): METZ
070 01-03 22:32:03.209: INFO/System.out(18231): MONTAUBAN
071 01-03 22:32:03.209: INFO/System.out(18231): MONT-DE-MARSAN
072 01-03 22:32:03.209: INFO/System.out(18231): MONTPELLIER
073 01-03 22:32:03.209: INFO/System.out(18231): MOULINS
074 01-03 22:32:03.230: INFO/System.out(18231): NANCY
075 01-03 22:32:03.230: INFO/System.out(18231): NANTERRE
076 01-03 22:32:03.239: INFO/System.out(18231): NANTES
077 01-03 22:32:03.239: INFO/System.out(18231): NARBONNE
078 01-03 22:32:03.249: INFO/System.out(18231): NEVERS
079 01-03 22:32:03.249: INFO/System.out(18231): NICE
080 01-03 22:32:03.249: INFO/System.out(18231): N??MES
081 01-03 22:32:03.249: INFO/System.out(18231): NIORT
082 01-03 22:32:03.259: INFO/System.out(18231): ORL??ANS
083 01-03 22:32:03.259: INFO/System.out(18231): PARIS
084 01-03 22:32:03.259: INFO/System.out(18231): PAU
085 01-03 22:32:03.259: INFO/System.out(18231): P??RIGUEUX
086 01-03 22:32:03.269: INFO/System.out(18231): PERPIGNAN
087 01-03 22:32:03.269: INFO/System.out(18231): POITIERS
088 01-03 22:32:03.269: INFO/System.out(18231): PONTOISE
089 01-03 22:32:03.280: INFO/System.out(18231): PRIVAS
090 01-03 22:32:03.280: INFO/System.out(18231): QUIMPER
091 01-03 22:32:03.280: INFO/System.out(18231): REIMS
092 01-03 22:32:03.289: INFO/System.out(18231): RENNES
093 01-03 22:32:03.289: INFO/System.out(18231): RODEZ
094 01-03 22:32:03.289: INFO/System.out(18231): ROUEN
095 01-03 22:32:03.309: INFO/System.out(18231): SAINT GERMAIN EN LAYE
096 01-03 22:32:03.309: INFO/System.out(18231): SAINT-BRIEUC
097 01-03 22:32:03.319: INFO/System.out(18231): SAINT-??TIENNE
098 01-03 22:32:03.329: INFO/System.out(18231): STRASBOURG
099 01-03 22:32:03.329: INFO/System.out(18231): TARBES
100 01-03 22:32:03.329: INFO/System.out(18231): TOULON
101 01-03 22:32:03.339: INFO/System.out(18231): TOULOUSE
102 01-03 22:32:03.339: INFO/System.out(18231): TOURS
103 01-03 22:32:03.339: INFO/System.out(18231): TROYES
104 01-03 22:32:03.339: INFO/System.out(18231): TULLE
105 01-03 22:32:03.339: INFO/System.out(18231): VALENCE
106 01-03 22:32:03.339: INFO/System.out(18231): VANNES
107 01-03 22:32:03.349: INFO/System.out(18231): VERDUN
108 01-03 22:32:03.349: INFO/System.out(18231): VERSAILLES
109 01-03 22:32:03.369: INFO/System.out(18231): VESOUL
110 01-03 22:32:03.379: INFO/System.out(18231): VIRE
111 01-03 22:32:04.089: INFO/ActivityManager(68): Displayed com.android.ImportDatabase/.TaxiActivity: +1s548ms