search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

[Android] SQLite資料庫使用

示範

XML

xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    >

    <EditText
        android:id="@+id/ed1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <EditText
        android:id="@+id/ed2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed1" />

    <EditText
        android:id="@+id/ed3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed2" />

    <EditText
        android:id="@+id/ed4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ed3" />

    <TextView
        android:id="@+id/t1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginStart="8dp"
        android:text="TextView"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:onClick="click"
        android:text="存資料"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />
android.support.constraint.ConstraintLayout>

JAVA

public class MainActivity extends AppCompatActivity {
SQLiteDatabase db; //資料庫宣告
Cursor cc;//查詢資料庫宣告
    static final String db_name="testDB"; // 資料庫名稱
    static final String tb_name="test"; // 資料表名稱
    EditText ed1,ed2,ed3,ed4;
    TextView t1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ed1=(EditText)findViewById(R.id.ed1);
        ed2=(EditText)findViewById(R.id.ed2);
        ed3=(EditText)findViewById(R.id.ed3);
        ed4=(EditText)findViewById(R.id.ed4);
        t1=(TextView)findViewById(R.id.t1);
        db = openOrCreateDatabase(db_name, Context.MODE_PRIVATE, null);
        String createTable="CREATE TABLE IF NOT EXISTS " +
                tb_name + // 資料表名稱
                "(number1 VARCHAR(100), " +
                "number2 VARCHAR(100), " +
                "number3 VARCHAR(100), " +
                "number4 VARCHAR(100))";
        db.execSQL(createTable); // 建立資料表

    }
    public void click(View v){
        if(!ed1.getText().equals("")&&!ed2.getText().equals("")&&!ed3.getText().equals("")&&!ed4.getText().equals("")) {
            db = openOrCreateDatabase(db_name, Context.MODE_PRIVATE, null);
            addData(ed1.getText().toString(), ed2.getText().toString(), ed3.getText().toString(), ed4.getText().toString());
            cc = db.rawQuery("SELECT * FROM " + tb_name, null);    // 重新查詢
            if (cc.moveToFirst()) {    // 移到第 1 筆資料 (若有資料才繼續)
                String str = "";
                do {        // 逐筆讀出資料
                    str += "number1:" + cc.getString(0) + "\n";
                    str += "number2:" + cc.getString(1) + "\n";
                    str += "number3:" + cc.getString(2) + "\n";
                    str += "number4:" + cc.getString(3) + "\n";
                    t1.setText(""+str);//將資料印出來
                } while (cc.moveToNext());    // 有一下筆就繼續迴圈
            }
            db.close();
        }
    }

    private void addData(String s1, String s2, String s3,String s4) {
        ContentValues cv=new ContentValues(4); // 建立含4個資料項目的物件
        cv.put("number1", s1);
        cv.put("number2", s2);
        cv.put("number3", s3);
        cv.put("number4", s4);
        db.insert(tb_name, null, cv); // 將資料加到資料表
    }
}

 

展示

相關文章:

[Android] Android 學習總集



熱門推薦

本文由 kk665403pixnetnetblog 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦