我用这种方式使用java创建了db:
public static void main(String[] args) throws Exception {
String CITIES[] = city.split(",");
Class.forName("org.sqlite.JDBC");
Connection conn = DriverManager.getConnection("jdbc:sqlite:test.db");
String V[][] = {COUNTRIES,CITIES,occupations};
String []TypeNames = {"country","city","occupation"};
Statement stat = conn.createStatement();
stat.executeUpdate("drop table if exists "+TABLE_NAME+";");
//stat.executeUpdate("create table "+TABLE_NAME+" (name, occupation);");
stat.executeUpdate("create table "+TABLE_NAME+" ("+VALUE+","+TYPE+","+LETTER+");");
PreparedStatement prep = conn.prepareStatement(
"insert into "+TABLE_NAME+" values (?, ?,?);");
//private void insertToTalble();
for(int j = 0 ;j < V.length; j++)
for (int i = 0 ;i < V[j].length ; i++)
{
Character c = V[j][i].charAt(0);
prep.setString(1, V[j][i]+"\n");
prep.setString(2, TypeNames[j]);
prep.setString(3, c.toString());
prep.addBatch();
}
conn.setAutoCommit(false);
prep.executeBatch();
conn.setAutoCommit(true);
rs.close();
conn.close();
}
}
当我使用sqllight数据浏览器打开它时它工作正常但是在我的android项目中将它添加到new diractory中名为databases / test1.db
我在使用它时遇到了问题
我使用数据库的android类是:
private static final String DB_PATH = "/data/data/com.countryCityGame/databases/test.db";
private static final String DATABASE_NAME = "test1.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "GameTable";
private static final String VALUE = "value";
private static final String TYPE = "type";
private static final String LETTER = "letter";
public countryCityGameLogic(EditText[] myEditTextArr , Context context){
this.context = context;
openHelper = new OpenHelper(context);
gameList = new CharSequence [myEditTextArr.length];
for (int i = 0 ; i < myEditTextArr.length; i++){
gameList[i] = myEditTextArr[i].getText();
}
this.db = openHelper.getWritableDatabase();
try{
String myPath = DB_PATH ;
SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}catch(SQLiteException e){
//database does't exist yet.
//insertValus(COUNTRIES,0);
}
//insertValus(COUNTRIES,0);
}
public void setGameVar(EditText[] myEditTextArr) {
for (int i = 0 ; i < myEditTextArr.length ;i++)
gameList[i] = myEditTextArr[i].getText();
}
private void insertValus(String []typeInserted , int num) {
ContentValues initialValues = new ContentValues();
for (int i = 0 ; i < typeInserted.length ; i++){
Character tmp = (Character)typeInserted[i].charAt(0);
initialValues.put(VALUE, typeInserted[i]);
initialValues.put(TYPE, TYPESNAMES[num]);
initialValues.put(LETTER,tmp.toString(tmp));
db.insert(TABLE_NAME, null, initialValues);
}
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " ( "+ VALUE +" TEXT,"+ TYPE +" TEXT, "+ LETTER + " TEXT)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
public boolean existInDataBase() {
boolean returnval = true;
String s = asUpperCaseFirstChar(gameList[0].toString());
Cursor cursor = db.query(TABLE_NAME, new String[] {VALUE}
,VALUE+" like " + "'%" + s +"%'", null, null, null, null);
if ( cursor.moveToFirst() == false)
returnval = false;
return returnval;
}
我没有在existInDataBase()函数的光标中获取任何信息 更具体一点我总是得到cursor.moveToFirst()的错误,即使我的查询只是选择没有任何东西
有人可以请: 告诉我他认为是错的 2.我可以调试并查看数据库中的内容(我调试但我看不出有什么奇怪的,如果我说有“你没有数据库”的话)注意“当我第一次构建应用程序时,应用程序是构建数据库的人,正如您在
中看到的那样 private void insertValus(String []typeInserted , int num);
我的问题似乎是清单文件:
当一个人获取一个db文件并将其导入到他的android项目中时他该怎么办?请解释一下我应该做什么,谢谢yoav。
答案 0 :(得分:0)
答案 1 :(得分:0)
我认为使用SQLiteOpenHelper在android中使用SQLite数据库是最简单的方法。您可以阅读this tutorial以了解并申请。这非常有帮助,我遵循它并且运行良好。