main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context=".MainActivity" >    <Button        android:id="@+id/button1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="插入数据" />    <Button        android:id="@+id/button2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="读取数据" />    <Button        android:id="@+id/button3"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="修改数据" />    <Button        android:id="@+id/button4"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="删除数据" /></LinearLayout>

DBOpenHelper.java

package com.example.sqlitedemo3;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class DBOpenHelper extends SQLiteOpenHelper{    //实例化时,指定创建数据库的名称    public DBOpenHelper(Context context, String name) {        super(context, name, null, 1);    }    @Override  //当第一次创建数据库时调用,系统自动调用,只执行一次    public void onCreate(SQLiteDatabase db) {        //第一次执行时,创建tb_user表        db.execSQL("create table if not exists tb_user(id integer primary key autoincrement,name text not null,age integer not null,sex text not null)");    }    @Override  //当数据库版本发生改变时调用,系统自动调用    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {            }}

main.java

package com.example.sqlitedemo3;import android.os.Bundle;import android.app.Activity;import android.content.ContentValues;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.util.Log;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.Toast;public class MainActivity extends Activity {    Button btnAdd;    Button btnQuery;    Button btnUpdate;    Button btnDelete;    SQLiteDatabase db;    DBOpenHelper helper;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //创建DBOpenHelper对象,第一个参数为调用的activity,第二个参数为数据库的名称        helper = new DBOpenHelper(MainActivity.this, "user.db");                btnAdd = (Button) findViewById(R.id.button1);        btnQuery = (Button) findViewById(R.id.button2);        btnUpdate = (Button) findViewById(R.id.button3);        btnDelete = (Button) findViewById(R.id.button4);        btnAdd.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                                //只是可写的数据库对象                db = helper.getWritableDatabase();                                ContentValues values = new ContentValues();                values.put("name", "李四");                values.put("age", 20);                values.put("sex", "女");                                db.insert("tb_user", null, values);                values.clear();                                values.put("name", "王五");                values.put("age", 22);                values.put("sex", "男");                                db.insert("tb_user", null, values);                Toast.makeText(MainActivity.this, "添加成功", 1).show();                                db.close();            }        });        btnQuery.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                //只是可写的数据库对象                db = helper.getReadableDatabase();                //其他方法和前面没有区别                Cursor cur = db.query("tb_user", new String[]{"id","name","age","sex"}, null, null, null, null, null);                while(cur.moveToNext()){                    int id = cur.getInt(0);                    String name = cur.getString(cur.getColumnIndex("name"));                    int age = cur.getInt(cur.getColumnIndex("age"));                    String sex = cur.getString(cur.getColumnIndex("sex"));                                        Log.i("stuinfo",id + ","+ name + "," + age + "," + sex);                }                cur.close();                db.close();            }        });        btnUpdate.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                //修改数据                db = helper.getWritableDatabase();                ContentValues values = new ContentValues();                values.put("name", "张三丰");                int result = db.update("tb_user", values, "id=?", new String[]{"1"});                if(result > 0)                    Toast.makeText(MainActivity.this, "修改成功", 1).show();                else                    Toast.makeText(MainActivity.this, "修改失败", 1).show();                db.close();            }        });        btnDelete.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                db = helper.getWritableDatabase();                //删除数据                int result = db.delete("tb_user", "id=?", new String[]{"1"});                if(result > 0)                    Toast.makeText(MainActivity.this, "删除成功", 1).show();                else                    Toast.makeText(MainActivity.this, "删除失败", 1).show();                db.close();            }        });    }}

更多相关文章

  1. Android 数据查询query函数参数解析
  2. Android向服务器提交数据(方式:get、post、AsyncHttpClient )
  3. Android 登陆、提交数据或者加载数据时提示页面
  4. Android中解析json数据的方式之一:Gson
  5. Android应用开发基础之数据存储和界面展现(一)
  6. Android Bluetooth蓝牙开发:Bluetooth蓝牙设备之间数据传输(4)
  7. 利用JDBC连接服务器数据库(Android)
  8. Android应用数据存储几种方式(3)

随机推荐

  1. phpMyAdmin的安装配置
  2. thinkphp5 编辑时 唯一验证 解决办法
  3. 无法从mysql中选择最新的而不是相同的数
  4. curl POST的数据大于1024字节
  5. PHP如何区分继承链中的$ this指针?
  6. 请问做PHP相关工作是不是基本都要懂前端
  7. php的冷门函数之——call_user_func_arra
  8. 快速使用数组的最近元素来确定新元素是否
  9. php中三种数据库的连接方式
  10. php是如何工作的