DevOps85
DevOps85

Reputation: 6523

Sqlite create db with multi tables from raw file

I want to create a DB with 2 tables in Sqlite. For 1 table it work but 2 not. I have in my raw folder one create_schema.sql file with:

CREATE TABLE USER_USA (
    _id integer PRIMARY KEY NOT NULL UNIQUE,
    user varchar NOT NULL UNIQUE,
    pass varchar NOT NULL UNIQUE,
    cod varchar
    );

CREATE INDEX UNIQUE_ENTRY_DATE ON USER_USA (_id);

and i have this:

   if (mDb == null || !mDb.isOpen()) {

            final File dbFile = mContext.getDatabasePath(MyDbClass.DB_NAME);
            final boolean wasExisting = dbFile.exists();
            if (!wasExisting) {
                // We read the sql to create the DB
                final String createDbStatement;
                try {
                    createDbStatement = ResourceUtils.getRawAsString(mContext, R.raw.create_schema);

                    // If it's not present we create it
                    DatabaseUtils.createDbFromSqlStatements(mContext, MyDbClass.DB_NAME, DB_VERSION, createDbStatement);

                } catch (IOException e) {
                    e.printStackTrace();
                    Log.e(TAG_LOG, "Error creating DB", e);
                    return;
                }
            }

            mDb = mContext.openOrCreateDatabase(MyDbClass.DB_NAME, Context.MODE_PRIVATE, null);

        }

Now if i try to modify the create_schema.sql file like below not work:

  CREATE TABLE USER_USA (
        _id integer PRIMARY KEY NOT NULL UNIQUE,
        user varchar NOT NULL UNIQUE,
        pass varchar NOT NULL UNIQUE,
        cod varchar
        );
CREATE TABLE GOOGLE_KEY (
        _id integer PRIMARY KEY NOT NULL UNIQUE,
        regid varchar NOT NULL UNIQUE,

        );
CREATE INDEX UNIQUE_ENTRY_DATE ON USER_USA (_id);
CREATE INDEX UNIQUE_ENTRY_DATE ON GOOGLE_KEY (_id);

where i'm wrong?

Error:

android.database.sqlite.SQLiteException: no such table: GOOGLE_KEY (code 1): , while compiling: SELECT * FROM GOOGLE_KEY

Upvotes: 0

Views: 115

Answers (1)

taxidev
taxidev

Reputation: 97

This is an improper ending to a create table statement:

CREATE TABLE GOOGLE_KEY (
    _id integer PRIMARY KEY NOT NULL UNIQUE,
    regid varchar NOT NULL UNIQUE,

    );

There is a hanging comma there, so the table won't be constructed. Look at the ending of your first create table statement, and fix this one to look like that one.

Upvotes: 1

Related Questions