创建表失败

时间:2012-09-21 15:53:57

标签: sqlite android-sqlite

我失败了所有的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;
}

}

1 个答案:

答案 0 :(得分:-1)

这里有很多评论:

一:

如果您坚持在SQLiteOpenHelper之外创建数据库。然后使用execQuery()而不是rawQuery()。我不知道这是否会解决问题,但这是你应该用于不返回结果的SQL查询的方法。

两个

首选在SQLiteOpenHelper内创建数据库,让它管理数据库的创建和更新。

如果您想在应用程序中使用预先加载的数据库,请参阅this blog post