1

If we already have a database file in .sql format, how can we load this in our android app & use its data?

I also had this same problem and I did what jaydeep said. But I m not able to proceed..Please help me. I added 1 method in this to retrieve 1 record which is as follows:

public Cursor getTitle(String g,String k) throws SQLException { 
    Cursor c = myDataBase.rawQuery(
      "select * from titles where food LIKE '%"+g+"%' and area LIKE '%"+k+"%' ",
    null); 
    if (c != null) {
        c.moveToFirst(); 
    }
    return c;
}

where titles is id name of table in database n food area n rest are fields. and in main file i did as follows:

{
    Datahelper myDbHelper = new Datahelper(this); 
    final String tem="Chinese"; 
    final String tem2="Bur Dubai"; 

    final ListView list = (ListView)findViewById(R.id.list);

    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }

    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
    Cursor c = myDbHelper.getTitle(tem,tem2);

    if (c.moveToFirst()) DisplayTitle(c);
    else Toast.makeText(this, "No title found", 
      Toast.LENGTH_LONG).show();
}

public void DisplayTitle(Cursor c) {
    final TextView ter=(TextView)findViewById(R.id.ter); 
    c.moveToPosition(0);
    while (c.isAfterLast() == false) {
        ter.append(c.getString(3)+"\n");
        c.moveToNext();
    }
    c.close();
}

Please help me.

ListView code:

if (c.moveToFirst()) 
{ 
final String [] restarr=c.getColumnNames(); 

String[] fields = new String[] {db.KEY_REST}; 

list.setAdapter(new ArrayAdapter<String>(this, R.layout.main, restarr)); 

SimpleCursorAdapter rest = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,fields, new int[] {android.R.id.text1}); 

list.setAdapter(rest); 
}

1 Answer 1

2

You need SQLite version of the database. I bet your current database you want to ship to the application, has schema written with pure SQL syntax which quite different from SQLite.

If my assumption is true, then you need to extract the schema and convert it to SQLite. This takes some minor changes in the syntax but logic stays the same.

Here is nice tutorial how you can ship your external local database in your android application:

http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

Mind the version and extension of the database file.

Sign up to request clarification or add additional context in comments.

6 Comments

yes i followd the same link which u gave jus added the get title method .n the changes in main file whcih i mentioned in a question.i m not at all getting wats gng wrong
try opening the database like this SQLiteDatabase db = SQLiteDatabase.openDatabase(getDatabasePath(DBNAME), null, SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.OPEN_READWRITE); instead using DBhelper class openDatabase() method
hey thnx man...it wrkd ..but now problem is coming in binding data to listview here is d code snippet which i did if (c.moveToFirst()) { final String [] restarr=c.getColumnNames(); String[] fields = new String[] {db.KEY_REST}; list.setAdapter(new ArrayAdapter<String>(this, R.layout.main, restarr)); SimpleCursorAdapter rest = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,fields, new int[] {android.R.id.text1}); list.setAdapter(rest); } here for fields array there is prob as i cant use this db.col name
new ArrayAdapter<String>(this, R.layout.main, restarr) why you are using your main layout? Why you are using another adapter? Create new one for the item views of the listview. The rest of code seems reasonable to me. These are my only remarks.
problem is while creating restarr adapter only.as i need parrlel database column for binding data which gets stored in fields array.so i tired like String[] fields = new String[] {myDbHelper.myDataBase.rest };(rest is col name) but it gvs error
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.