Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLite Database Browser with Android

Tags:

android

sqlite

I'm confused as to what file formats are acceptable for SQLiteDatabase. I want to use my pre-existing database which I created in SQLite Database Browser. There are export options to an SQL, CSV, and text file. I've tried opening the file as a text file and SQL file in Notepad++, and all I see is a CREATE TABLE command with INSERTS (that I made through SQLite Database Browser). Is this the wrong file type? Or does SQLiteDatabase actually use this information to create the table? I've used these files with the code that has been referenced since 2009: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/ but the code at that link is not working for me. The file either cannot be opened for copying, or it can be opened but once a query is attempted I get an error saying that "table name" no such table can be found.

Code:

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

btw, this is the content of my database file.

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;

This is why I asked about file format. I've opened other sql, sqlite files before and what I get is binary gibberish. What I have here is a simple CREATE TABLE command.

like image 480
ShrimpCrackers Avatar asked Nov 12 '22 23:11

ShrimpCrackers


1 Answers

I made changes to two things. Using the CREATE_IF_NECESSARY flag for SQLite.openDatabase(...) and I realized I had the wrong file format. I was exporting to SQL or TXT instead of using the saved file when using the database browser.

like image 116
ShrimpCrackers Avatar answered Dec 24 '22 21:12

ShrimpCrackers