-2

我在 SQLite 中有一个 s3db 数据库。现在我想在应用程序中附加或打开它?请告诉我需要编写什么代码才能连接到数据库?

4

2 回答 2

1

I don't know exactly what you want but if you want to get data from your database you can do it like this:

this.database = new ContactsDatabase(this, Contacts.DB_NAME, null, Contacts.startVersion).getWritableDatabase();
        this.cursor = this.database.rawQuery("SELECT _id, name, email, phone FROM contacts ORDER BY name ASC", null);

Check official guides.

于 2012-05-24T09:18:54.960 回答
1

我会给你一个例子,你可以很容易地理解我们如何在文本视图上显示数据库中的数据。下面是其中 testdata 是具有用户表的数据库的名称的代码,其中存在 2 列。我在 2 diff 上显示这 2 列的数据。文本视图。

public class high_score extends Activity{
SQLiteDatabase mydatabase=null;
String Data="";
String Data1="";
String TableName="users";

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.high_score);




        RelativeLayout main=(RelativeLayout)findViewById(R.id.main);

        try
        {
            mydatabase=this.openOrCreateDatabase("testdata", MODE_PRIVATE, null);

            Cursor c=mydatabase.rawQuery("SELECT DISTINCT name,score FROM "+TableName+" , null);
            int column1=c.getColumnIndex("name");
            int column2=c.getColumnIndex("max_score");
            c.moveToFirst();
            if(c!=null)
            {   
                do
                {
                    String Name=c.getString(column1);
                    int Score=c.getInt(column2);
                    Data=Data+Name+"\n";
                    Data1=Data1+Score+"\n";
                    System.out.println("name"+Name+" score"+Score);
                }while(c.moveToNext());
                TextView tv=(TextView)findViewById(R.id.tv);
                TextView points=(TextView)findViewById(R.id.points);
                tv.setText(Data);
                points.setText(Data1);
                setContentView(main);

                }

             /*TextView tv=(TextView)findViewById(R.id.tv);
            tv.setText(Data);
            setContentView(tv);*/

        }catch(Exception ex)
        {
            Log.e("ERROR","ERROR" ,ex);
        }
        finally
        {
            if(mydatabase!=null)
                mydatabase.close();
        }

您可以像这样轻松访问数据

于 2012-05-24T09:30:10.950 回答