开发者

failing to connect to database (SQLite on android)

开发者 https://www.devze.com 2023-01-04 06:28 出处:网络
I have made db using java this way : public static void main(String[] args) throws Exception { String CITIES[] = city.split(\",\");

I have made db using java this way :

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();
  }
}

when i open it using sqllight data browser it works fine but after adding it to new diractory in my android project called databases/test1.db

I am having problems using it

my android class whom works with the data base is :

     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 CharSeque开发者_如何学JAVAnce [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;
    }

i am not getting any information in the cursor in existInDataBase() function to be more specific I'm always getting false for cursor.moveToFirst() even when my query is just to select without any thing

can someone please : 1.tell me what he thinks is wrong 2.how can i debug and see what there is in the db (i debug but i cant see anything odd should i had a if saying "you have no data base")

note " when i builded the app the first time the app was the the one who build the db as you can see in

 private void insertValus(String []typeInserted , int num);

my problem seems to be the manifest file:

when one takes a db file and import it to his android project what should he do ?please explain in steps what should i do thanks yoav.


Did you read this tutorial?

To see what in your database on device use "adb shell" command. From adb shell you can connect to database using sqlite3 command. Additional info can be found here


I think working with SQLite database in android using SQLiteOpenHelper is the easiest way. You can read this tutorial to understand and apply. It's very helpful, i followed it and it's work well.

0

精彩评论

暂无评论...
验证码 换一张
取 消