5

SQLiteDatabase で使用できるファイル形式について混乱しています。SQLite Database Browser で作成した既存のデータベースを使用したいと考えています。SQL、CSV、およびテキスト ファイルへのエクスポート オプションがあります。Notepad ++でファイルをテキストファイルとSQLファイルとして開いてみましたが、表示されるのはINSERTSを使用したCREATE TABLEコマンドだけです(SQLite Database Browserで作成しました)。これは間違ったファイルタイプですか?または、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 コマンドです。

4

3 に答える 3

1

私は2つのことを変更しました。SQLite.openDatabase(...) に CREATE_IF_NECESSARY フラグを使用すると、ファイル形式が間違っていることに気付きました。データベース ブラウザを使用しているときに、保存したファイルを使用する代わりに、SQL または TXT にエクスポートしていました。

于 2012-08-29T00:28:25.913 に答える
0

マニフェストファイルで許可を与えていない可能性があります。チュートリアルでは触れられていないと思います。これをマニフェストファイルに追加します。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
于 2012-08-27T09:48:00.593 に答える
0

db クラス + db ヘルパー クラスを作成する必要があります。

なぜ?開発目的では、エミュレーター内で db を使用できますが、デバイス (ルート化されていない場合) では、data/data フォルダーにアクセスできません。

ヘルパー クラスを作成するだけです。次に、データベースを作成し、更新時に管理します...

さらに 4 つの情報: http://www.vogella.com/articles/AndroidSQLite/article.html

編集:Linuxはマジックバイトを使用してファイルタイプを判別するため、任意のファイルタイプがサポートされています...SQLiteブラウザからデータベースを保存するときは、name.dbのように保存するか、単に名前を付けて保存します(SQLダンプではありません)

于 2012-08-27T09:50:23.243 に答える