使用Android的SQLite数据库浏览器

时间:2012-08-27 09:43:04

标签: android sqlite

我对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

    }

}

File definitely there

不过,这是我的数据库文件的内容。

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命令。

3 个答案:

答案 0 :(得分:1)

我改变了两件事。使用SQLite.openDatabase(...)的CREATE_IF_NECESSARY标志,我意识到我的文件格式错误。我在使用数据库浏览器时导出到SQL或TXT而不是使用保存的文件。

答案 1 :(得分:0)

它可能没有用,可能你没有在清单文件中给予权限。我认为教程没有提到它。将其添加到您的清单文件中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

答案 2 :(得分:0)

你需要创建一个db class + db helper类!

为什么呢? 出于开发目的,您可以在emelutor中使用db,但在设备上(如果它没有root)您无法访问数据/数据文件夹

只需创建一个帮助类。然后它会创建你的数据库,在更新等时管理它......

4更多inf: http://www.vogella.com/articles/AndroidSQLite/article.html

编辑:支持任何文件类型,因为linux使用魔术字节来确定文件类型...当你从SQLite浏览器保存数据库时,保存它就像name.db或只是名称(不是sql转储)