SQLliet的基本操作

1.布局文件.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    >

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="20dp">

        <TextView
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="名字:"
            android:textColor="#000"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/inputname"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:textColor="#000"

            />

        <EditText
            android:id="@+id/inputname2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:layout_marginLeft="20dp"
            android:textColor="#000"

            />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginTop="10dp">

        <TextView
            android:layout_width="80dp"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="年龄:"
            android:textColor="#000"
            android:textSize="20dp" />

        <EditText
            android:id="@+id/inputage"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:inputType="phone"
            android:textColor="#000"

            />

        <EditText
            android:id="@+id/inputage2"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            android:textColor="#000"
            android:layout_marginLeft="20dp"
            android:inputType="phone"

            />

    </TableRow>

    <TableRow
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="create"
            android:text="添加数据" />

        <Button
            android:id="@+id/up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="create"
            android:text="修改数据" />

        <Button
            android:id="@+id/selet"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="create"
            android:text="查询数据" />

        <Button
            android:id="@+id/dele"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:onClick="create"
            android:text="删除数据" />

    </TableRow>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_margin="10dp"
        android:id="@+id/show"
        android:padding="10dp"
        android:textColor="#f00"
        android:textSize="20dp"
        />


</LinearLayout>

2.定义一个类MyDatabaseHelper,继承SQLiteOpenHelper抽象类。

package topteam.com.sql_demo;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;

public class MyDatabaseHelper extends SQLiteOpenHelper {

    private static final int VERSION = 3;  //数据库的版本号
    private static final String CREATE_BOOK = "create table book(id integer primary key autoincrement,name text)"; //创建表1
    private static final String USERLIST= "create table user(id integer primary key autoincrement,name text,age integer)"; //创建表2

    /**
     *
     * @param context context
     * @param name  数据库名字
     */
    public MyDatabaseHelper(Context context, String name) {
        super(context, name,null, VERSION);
    }

    /**
     * 当数据库不存在,第一次创建,并且第一次使用的时候
     * @param db
     */
    @Override
    public void onCreate(SQLiteDatabase db) {
        //表只需要创建一次
            db.execSQL(USERLIST);

        Log.i("TEXT","onCreate");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.i("TEXT","onUpgrade"+oldVersion+"---"+newVersion);
    }
}

3.MainActivity.java程序主类

package topteam.com.sql_demo;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Context context;
    EditText inputname1_v;
    EditText inputname2_v;
    EditText inputage1_v;
    EditText inputage2_v;
    TextView show_v;
    Button add_v;
    Button up_v;
    Button sele_v;
    Button dele_v;
    MyDatabaseHelper databaseHelper;
    SQLiteDatabase db;
    String name1 = "";
    String name2 = "";
    int age1;
    int age2;
    StringBuilder sb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar bar = getSupportActionBar();
        if (bar != null) {
            bar.hide();
        }
        initView();
        context = MainActivity.this;
        //创建数据库
        databaseHelper = new MyDatabaseHelper(context, "usersql.db");

    }

    /**
     * 初始化控件
     */
    private void initView() {
        inputname1_v = findViewById(R.id.inputname);
        inputname2_v = findViewById(R.id.inputname2);
        inputage1_v = findViewById(R.id.inputage);
        inputage2_v = findViewById(R.id.inputage2);
        add_v = findViewById(R.id.add);
        add_v.setOnClickListener(this);
        up_v = findViewById(R.id.up);
        up_v.setOnClickListener(this);
        sele_v = findViewById(R.id.selet);
        sele_v.setOnClickListener(this);
        dele_v = findViewById(R.id.dele);
        dele_v.setOnClickListener(this);
        show_v = findViewById(R.id.show);
    }


    @Override
    public void onClick(View v) {
        db = databaseHelper.getWritableDatabase();
        switch (v.getId()) {
            case R.id.add:
                name1 = inputname1_v.getText().toString();
                String age = inputage1_v.getText().toString();
                if (!age.equals("")) {
                    age1 = Integer.parseInt(age);
                }
                if (name1.equals("") || age1 == 0) {
                    Toast.makeText(context, "录入信息不全", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    ContentValues values = new ContentValues();
                    values.put("name", name1);
                    values.put("age", age1);
                    db.insert("user", null, values);
                }
                show_v.setText("添加成功");
                break;
            case R.id.up:
                name1 = inputname1_v.getText().toString();
                age1 = Integer.parseInt(inputage1_v.getText().toString());
                ContentValues values2 = new ContentValues();
                values2.put("age", age1);
                db.update("user", values2, "name=?", new String[]{name1});
                show_v.setText("修改成功");
                break;
            case R.id.selet:
                sb = new StringBuilder();
                Cursor cursor = db.query(
                        "user",
                        null,
                        null,
                        null,
                        null,
                        null,
                        null
                );
                if (cursor.moveToFirst()) {
                    do {
                        String str = cursor.getString(cursor.getColumnIndex("name"));
                        int ag = cursor.getInt(cursor.getColumnIndex("age"));
                        sb.append(str + "  " + ag + "\n");
                    } while (cursor.moveToNext());
                }
                cursor.close();
                show_v.setText(sb.toString());
                break;
            case R.id.dele:
                name1 = inputname1_v.getText().toString();
                db.delete("user", "name=?", new String[]{name1});
                show_v.setText("删除成功");
                break;
            default:
                break;
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值