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 Lis开发者_JAVA技巧tView 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);
}
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.
精彩评论