我对 SQLiteDatabase 可以接受哪些文件格式感到困惑。我想使用我在 SQLite 数据库浏览器中创建的预先存在的数据库。SQL、CSV 和文本文件有导出选项。我尝试在 Notepad++ 中将文件作为文本文件和 SQL 文件打开,我看到的只是一个带有 INSERTS 的 CREATE TABLE 命令(我通过 SQLite 数据库浏览器创建的)。这是错误的文件类型吗?或者 SQLiteDatabase 是否真的使用这些信息来创建表?我已将这些文件与自 2009 年以来引用的代码一起使用:http ://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/但是该链接上的代码对我不起作用。该文件要么无法打开进行复制,要么可以打开,但一旦尝试查询,我就会收到一条错误消息,提示“表名”找不到这样的表。
代码:
public class DatabaseHelper extends SQLiteOpenHelper {
public final static int DB_VERSION = 1;
public final static String DB_PATH = "/data/data/seattle.tourists/databases/";
public final static String DB_NAME = "attractioninfo";
Context myContext;
public DatabaseHelper(Context context ) {
super( context, DB_NAME, null, DB_VERSION );
myContext = context;
}
public void createDatabase() {
boolean dbExists = databaseExists();
if ( !dbExists ) {
this.getReadableDatabase();
try {
copyDatabase();
}
catch ( IOException e ) {
System.out.println( "copy database error" );
}
}
}
private boolean databaseExists() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase( myPath, null, SQLiteDatabase.OPEN_READONLY );
}
catch ( SQLiteException e ) {
System.out.println( "database does not exist" );
}
return checkDB != null ? true : false;
}
private void copyDatabase() throws IOException {
InputStream myInput = myContext.getAssets().open( DB_NAME );
String outFileName = DB_PATH + DB_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 SQLiteDatabase openDatabase() throws SQLException {
// Open the database
String myPath = DB_PATH + DB_NAME;
if ( databaseExists() )
return SQLiteDatabase.openDatabase( myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS );
return null;
}
public synchronized void 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
}
}
顺便说一句,这是我的数据库文件的内容。
BEGIN TRANSACTION;
CREATE TABLE ExtraInfo (commonName , hours , fare , website , summary );
INSERT INTO ExtraInfo VALUES('Alki Beach Park','4 am - 11:30 pm',-1,-1,'Enjoy the Seattle skyline, ride bikes, or fly kites at Alki Beach.');
INSERT INTO ExtraInfo VALUES('Bradner Gardens','4 am - 11:30 pm',-1,-1,'A 1.6-acre park in the Mt. Baker neighborhood of southeast Seattle. Enjoy a garden, p-patch, basketball court and more.');
INSERT INTO ExtraInfo VALUES('Experience Music Project','Hours vary by season. See website for details.','Range by age: $14 - $20',-1,'Experience music hands on! Explore music history and get creative in the interactive Sound Lab.');
INSERT INTO ExtraInfo VALUES('Japanese Garden','Hours vary by season. See website.','Varies. See website.','http://www.seattle.gov/parks/parkspaces/japanesegarden.htm','Located within the Washington Park Arboretum, this is a 3.5 acre formal garden designed and constructed under the supervision of world-renowned Japanese garden designer Juki Iida in 1960.');
INSERT INTO ExtraInfo VALUES('Katie Blacks Garden','4 am - 11:30 pm',-1,-1,'This historic landscape was originally created for Katie Black, an early Seattle settler. The community rallied to preserve it and has worked hard to remove blackberries and restore the garden.');
INSERT INTO ExtraInfo VALUES('Kubota Garden','6 am - 10 pm',-1,-1,'Hidden in South Seattle, Kubota Garden is a stunning 20 acre landscape that blends Japanese garden concepts with native Northwest plants.');
INSERT INTO ExtraInfo VALUES('Museum Of Flight','Daily 10 am - 5 pm.\nClosed Thanksgiving and Christmas.','Adults $17\nSeniors (65+) $14\nYouth(5-17) $9\nChildren (4 and under) Free','http://www.museumofflight.org/','A museum dedicated to the history of air and space flight. Collection includes 150+ historically significant air- and spacecraft.');
INSERT INTO ExtraInfo VALUES('Pacific Science Center','Daily 9:45 am - 6 pm','Exhibits Admission\nAdults(16-64) $16\nSeniors(65+) $14\nYouth(6-15) $11\nKids(3-5) $9',-1,'Explore science hands on through a variety of different exhibits such as "Dinosaurs: A Journey Through Time" or the "Science Playground".');
INSERT INTO ExtraInfo VALUES('Parsons Garden','6 am - 10 pm',-1,-1,'Formerly the family garden of Reginald H. Parsons, the park was given to the City in 1956 by the family''s children. Often used for ceremonies, this mall but lovely garden is a hidden gem on Queen Anne''s south slope.');
INSERT INTO ExtraInfo VALUES('Science Fiction Museum','Hours vary by season. See website for details.','Range by age: $14 - $20',-1,'Explore science fiction through film and literature at the Science Fiction Museum. Browse the exhibits'' large collection of film and TV props and memorabilia, as well as books.');
INSERT INTO ExtraInfo VALUES('Seattle Aquarium','Daily 9:30 am - 5 pm\nThanksgiving and Christmas Day 9:30 am - 3 pm\nChristmas Day Closed','Adults (13+) $19.95\nYouth(4-12) $13.95\nChild(3 & under) Free','http://www.seattleaquarium.org/','Come see a variety of cute and strange aquatic creatures!');
INSERT INTO ExtraInfo VALUES('Seattle Childrens Theatre',-1,-1,-1,'Seattle Children''s Theatre provides children of all ages access to professional theatre, with a focus on new works, and theatre education.');
INSERT INTO ExtraInfo VALUES('Volunteer Park Conservatory','6 am - 10 pm',-1,-1,'Located in the heart of Seattle, Volunteer Park is home to the Volunteer Park Conservatory, Seattle Asian Art Museum, Puget Sound and downtown views, and more.');
INSERT INTO ExtraInfo VALUES('Washington Park Arboretum','Dawn to Dusk',-1,-1,'Managed by the UW and City of Seattle, this 230 acre arboretum has a dynamic assortment of plants found nowhere else.');
INSERT INTO ExtraInfo VALUES('Woodland Park Zoo Rose Garden','4 am - 11:30 pm',-1,-1,'A multipurpose park and recreation space southwest of Green Lake and north of the Fremont district. Ideal for picnics (reservable), BBQs, sports and recreation.');
COMMIT;
这就是我询问文件格式的原因。我之前打开过其他 sql、sqlite 文件,得到的是二进制乱码。我这里有一个简单的 CREATE TABLE 命令。