0

我失败了所有的 CREATE TABLE;和带有错误的 INSERT 语句no such a table

Android 2.3 不会发生这种情况。取而代之的是3.2。这是为什么?创建数据库时出错?

我的课堂示例:

public class MainActivity extends Activity {

DatabaseHelper databaseHelper = new DatabaseHelper(this);

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    String queryTest="create table TEST_TABLE(ID integer,T_MESSAGE text)";
    databaseHelper.executeQuery(queryTest);
    queryTest="INSERT INTO TEST_TABLE (ID,T_MESSAGE) VALUES (1,'1');";
    databaseHelper.executeQuery(queryTest);

}

}

我的数据库:

public class DatabaseHelper extends SQLiteOpenHelper {

private static String DATABASE_NAME = "pro.db";
private static String DATABASE_PATH = "/data/data/it.sysman.mobile.contactpro/databases/";



private static final int SCHEMA_VERSION = 2;

public DatabaseHelper(Context context) {
    super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqlDB) {
    try {



    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}


@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // TODO Auto-generated method stub

}
private SQLiteDatabase openDataBase(int flag) {

    SQLiteDatabase checkDB = null;

    try {

        Environment.getExternalStorageDirectory();
        Environment.getDataDirectory();
        Environment.getRootDirectory();
        Environment.getExternalStorageState();
        checkDB = getWritableDatabase();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return checkDB;
}


public String executeQuery(String query) {

    SQLiteDatabase sqlDB = null;
    Cursor data = null;
    try {
        sqlDB = openDataBase(SQLiteDatabase.NO_LOCALIZED_COLLATORS);
        data = sqlDB.rawQuery(query, null);

        if (data.getCount() == 0)
            return null;

    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (data != null)
            data.close();
        if (sqlDB != null)
            sqlDB.close();
    }
    return null;
}

}

4

1 回答 1

-1

there are a number of remarks here:

One:

If you insist on creating the database outside of SQLiteOpenHelper. then use execQuery() and not rawQuery(). I don't know if this will solve the issue, but this is the method you should use for sql queries that does not return results.

Two:

It is prefered to do the creation of your database inside the SQLiteOpenHelper and let it manage the creation and updating of your database.

see this blog post if you want to use a pre-loaded database with your application.

于 2012-09-25T10:40:52.837 回答