如何连接到.sql文件中定义的现有数据库?

时间:2011-08-29 12:31:19

标签: android sqlite

如果我们已经有.sql格式的数据库文件,我们如何在我们的Android应用程序&使用它的数据?

我也有同样的问题,我做了jaydeep所说的。但是我无法继续......请帮助我。我在这里添加了1个方法来检索1条记录,如下所示:

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

其中titles是数据库中的表的id名称n食物区域n rest是字段。在主文件中我做了如下:

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

请帮帮我。

ListView代码:

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 个答案:

答案 0 :(得分:2)

您需要SQLite版本的数据库。我打赌你想要发送到应用程序的当前数据库,使用纯SQL语法编写的模式与SQLite完全不同。

如果我的假设是正确的,那么您需要提取架构并将其转换为SQLite。这需要对语法进行一些小的改动,但逻辑保持不变。

这是一个很好的教程,如何在Android应用程序中发布外部本地数据库:

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

注意数据库文件的版本和扩展名。

相关问题