获取Android中的SQLite数据库的数据?数据库、数据、Android、SQLite

2023-09-05 05:21:39 作者:坠红颜君故

林新到Android我如何reate两粒扣的时候我preSS它关系到下一个页面的第一个按钮,让用户输入并存储到SQLite数据库和preSS第二个按钮,它获取存储来自数据库的数据,并在tableview中显示的数据

Im new to android i how to reate two button when i press the first button it goes to next page and get the user input and stored it into the sqlite database and press the second button it fetch the stored data from the database and display the data in tableview

推荐答案

研究这个例子,它从sqlite的纪录显示,一个简单的教程。

Study this example, it displays records from the sqlite, a simple tutorial.

import java.util.ArrayList;

import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.widget.ArrayAdapter;

public class CRUDonDB extends ListActivity {

    private final String SAMPLE_DB_NAME = "myFriendsDb";
    private final String SAMPLE_TABLE_NAME = "friends";

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        ArrayList<String> results = new ArrayList<String>();
        SQLiteDatabase sampleDB = null;

        try {
            sampleDB =  this.openOrCreateDatabase(SAMPLE_DB_NAME, MODE_PRIVATE, null);

            sampleDB.execSQL("CREATE TABLE IF NOT EXISTS " +
                    SAMPLE_TABLE_NAME +
                    " (LastName VARCHAR, FirstName VARCHAR," +
                    " Country VARCHAR, Age INT(3));");

            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Makam','Sai Geetha','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Chittur','Raman','India',25);");
            sampleDB.execSQL("INSERT INTO " +
                    SAMPLE_TABLE_NAME +
                    " Values ('Solutions','Collabera','India',20);");

            Cursor c = sampleDB.rawQuery("SELECT FirstName, Age FROM " +
                    SAMPLE_TABLE_NAME +
                    " where Age > 10 LIMIT 5", null);

            if (c != null ) {
                if  (c.moveToFirst()) {
                    do {
                        String firstName = c.getString(c.getColumnIndex("FirstName"));
                        int age = c.getInt(c.getColumnIndex("Age"));
                        results.add("" + firstName + ",Age: " + age);
                    }while (c.moveToNext());
                } 
            }

            this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,results));

        } catch (SQLiteException se ) {
            Log.e(getClass().getSimpleName(), "Could not create or Open the database");
        } finally {
            if (sampleDB != null) 
                sampleDB.execSQL("DELETE FROM " + SAMPLE_TABLE_NAME);
                sampleDB.close();
        }
    }
}
 
精彩推荐
图片推荐