SQLiteOpenHelper学习笔记

来源:互联网 发布:网络算命大师 编辑:程序博客网 时间:2024/05/14 20:16
<?xml version="1.0" encoding="utf-8"?><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:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    android:orientation="vertical"    tools:context="com.user.android2_lesson5_sqlitedatabasehelper.MainActivity"><Button    android:id="@+id/create"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="创建表"/>    <Button        android:id="@+id/insert"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:text="添加数据"/>    <Button        android:id="@+id/delete"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="删除数据"/>    <Button        android:id="@+id/updata"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="修改数据"/>    <Button        android:id="@+id/select"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="查询数据"/></LinearLayout>创建一个datahelper继承SQLiteOpenHelperpackage com.user.android2_lesson5_sqlitedatabasehelper;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;/** * Created by user on 2016/7/8. */public class MyDataHelper extends SQLiteOpenHelper {//    构造方法 这个方法必须要有//    public MyDataHelper(Context context,String name ,SQLiteDatabase.CursorFactory factory,int version){        super(context,name,factory,version);    }//    当第一次创建数据库的时候会调用该方法    @Override    public void onCreate(SQLiteDatabase sqLiteDatabase) {        Log.i("DataHelper","创建数据库,初始化数据库");//        因为是第一次数据库创建的时候调用的,所以这里通常进行的是数据库表的创建        String sql = "CREATE TABLE IF NOT EXISTS student(sid int,name text,sex text,age int)";//        执行表创建语句        sqLiteDatabase.execSQL(sql);    }//    当更新数据库(数据库版本更新)的时候调用该方法    @Override    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {     Log.i("DataHelper","更新数据库");    }}package com.user.android2_lesson5_sqlitedatabasehelper;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity {    private Button create;    private Button insert;    private Button delete;    private Button updata;    private Button select;    private SQLiteDatabase db;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        create = (Button) findViewById(R.id.create);        insert = (Button) findViewById(R.id.insert);        delete = (Button) findViewById(R.id.delete);        updata  = (Button) findViewById(R.id.updata);        select = (Button) findViewById(R.id.select);        create.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {//                创建Helper对象                MyDataHelper helper = new MyDataHelper(MainActivity.this,"student_data2",null,1);//                获取一个可读的数据库对象                 db = helper.getReadableDatabase();            }        });//        插入数据        insert.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {//                这里注意处理好版本号                MyDataHelper helper = new MyDataHelper(MainActivity.this,"student_data2",null,2);//                得到一个可写入的数据库                 db = helper.getWritableDatabase();////                ContentValues con = new ContentValues();//                con.put("sid",1);//                con.put("name","张三");//                con.put("sex","男");//                con.put("age",18);//                db.insert("student",null,con);//                或者这样写                String sql  = "INSERT INTO student(sid,name,sex,age) VALUES(2,'李三炮','人妖',99); ";                db.execSQL(sql);//                关闭数据库                Log.i("123","321");                db.close();            }        });        delete.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                String sql = "DELETE FROM student where sid = 1;";                db.execSQL(sql);               Log.i("1","删除成功");            }        });        updata.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                MyDataHelper helper = new MyDataHelper(MainActivity.this,"student_data2",null,2);                db = helper.getReadableDatabase();            }        });        select.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                Cursor cursor = db.query("student",null,null,null,null,null,null);                Log.i("2","ssss");                if (cursor.moveToFirst()){                    do {                   int sid = cursor.getInt(0);                    String name = cursor.getString(1);                        String sex = cursor.getString(2);                        int age = cursor.getInt(3);                    }while(cursor.moveToNext());                }            }        });    }}
0 0
原创粉丝点击