SQLite3的简单使用

来源:互联网 发布:会计软件有哪些 编辑:程序博客网 时间:2024/05/29 03:20

一、布局文件:

<?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:id="@+id/activity_main"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical"    tools:context="com.example.mao.sqlite3demo.MainActivity">    <EditText        android:id="@+id/edt_name"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入姓名"/>    <EditText        android:id="@+id/edt_age"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:hint="请输入年龄"/>    <LinearLayout        android:layout_width="match_parent"        android:layout_height="wrap_content">        <Button            android:id="@+id/btn_insert"            android:layout_weight="1"            android:layout_width="0dip"            android:layout_height="wrap_content"            android:text="增加"/>        <Button            android:id="@+id/btn_delete"            android:layout_weight="1"            android:layout_width="0dip"            android:text="删除"            android:layout_height="wrap_content" />        <Button            android:id="@+id/btn_update"            android:layout_weight="1"            android:layout_width="0dip"            android:text="更新"            android:layout_height="wrap_content" />        <Button            android:id="@+id/btn_query"            android:layout_weight="1"            android:text="查询"            android:layout_width="0dip"            android:layout_height="wrap_content" />    </LinearLayout></LinearLayout>

二、MainActivity

package com.example.mao.sqlite3demo;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.Toast;/** SQLite3测验* */public class MainActivity extends AppCompatActivity implements View.OnClickListener {    EditText edt_name,edt_age;    Button btn_insert,btn_delete,btn_update,btn_query;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();        //注册监听器        btn_insert.setOnClickListener(this);        btn_delete.setOnClickListener(this);        btn_update.setOnClickListener(this);        btn_query.setOnClickListener(this);    }    //实例化控件    private void init() {        edt_name=(EditText)findViewById(R.id.edt_name);        edt_age= (EditText) findViewById(R.id.edt_age);        btn_insert=(Button)findViewById(R.id.btn_insert);        btn_delete=(Button)findViewById(R.id.btn_delete);        btn_update=(Button)findViewById(R.id.btn_update);        btn_query=(Button)findViewById(R.id.btn_query);    }    @Override    public void onClick(View view) {        SQlite3Utils sQlite3Utils=new SQlite3Utils(MainActivity.this);        switch (view.getId())        {            case R.id.btn_insert:                String age=edt_age.getText().toString();                String name=edt_name.getText().toString();                edt_name.setText("");                edt_age.setText("");              sQlite3Utils.insertDate(name,age);                Toast.makeText(MainActivity.this,"insert successfulily",Toast.LENGTH_LONG).show();                break;            case R.id.btn_delete:                name = edt_name.getText().toString();                sQlite3Utils.deleteDate(name);                edt_name.setText("");                Toast.makeText(MainActivity.this,"delete successfulily",Toast.LENGTH_LONG).show();                break;            case R.id.btn_update:                age = edt_age.getText().toString();                name = edt_name.getText().toString();                sQlite3Utils.updateDate(age,name);                edt_name.setText("");                edt_age.setText("");                Toast.makeText(MainActivity.this,"update successfulily",Toast.LENGTH_LONG).show();                break;            case R.id.btn_query:                name = edt_name.getText().toString();                String rage=sQlite3Utils.queryDate(name);                edt_name.setText("");                Toast.makeText(MainActivity.this,"name="+name+"--age="+rage,Toast.LENGTH_LONG).show();                break;        }    }}
 

三、SQLITE3Utils(自定义的具类)

package com.example.mao.sqlite3demo;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;/** * Created by mao on 2017/8/25. */public class SQlite3Utils  {    MyOpenHelper helper=null;    SQLiteDatabase db;    Context mcontext;    public SQlite3Utils(Context context)    {        mcontext=context;        helper=new MyOpenHelper(mcontext);    }    //增加数据的方法    public void insertDate(String insertName,String insertAge){        //调用该方法时,系统会调用MyOpenHelper onCreate()方法创建一个数据库并返回该数据库对象        db=helper.getWritableDatabase();        //执行插入语句        //db.execSQL("insert into user(name,age) values (?,?);",new Object[]{insertName,insertAge});        //android同样为我们提供了有关数据库的增删改查的方法,让我们可以在不熟悉SQL语句的条件下完成对数据库的操作        ContentValues values=new ContentValues();        values.put("name",insertName);        values.put("age",insertAge);        db.insert("user",null,values);        db.close();    }    //删除数据的方法    public void deleteDate(String name){        //调用该方法时,系统会调用MyOpenHelper onCreate()方法创建一个数据库并返回该数据库对象        db=helper.getWritableDatabase();        //执行删除语句        //db.execSQL("delete from user where name=?;",new Object[]{name});        //android同样为我们提供了有关数据库的增删改查的方法,让我们可以在不熟悉SQL语句的条件下完成对数据库的操作        db.delete("user","name=?",new String[]{name});        db.close();    }    //更新数据的方法    public void updateDate(String age,String name){        //调用该方法时,系统会调用MyOpenHelper onCreate()方法创建一个数据库并返回该数据库对象        db=helper.getWritableDatabase();        //执行更新语句        //db.execSQL("update user set age=? where name=?;",new Object[]{age,name});        //android同样为我们提供了有关数据库的增删改查的方法,让我们可以在不熟悉SQL语句的条件下完成对数据库的操作        ContentValues values=new ContentValues();        values.put("age",age);        db.update("user",values,"name=?",new String[]{name});        db.close();    }    //查询数据的方法    public String queryDate(String name){        //调用该方法时,系统会调用MyOpenHelper onCreate()方法创建一个数据库并返回该数据库对象        db=helper.getWritableDatabase();        //执行删除语句       // Cursor curse=db.rawQuery("select age from user where name=?",new String[]{name}        Cursor curse=db.query("user",new String[]{"age"},"name=?",new String[]{name},null,null,null);        String mName=null;        while (curse.moveToNext())        {            mName=curse.getString(0);        }        db.close();        return  mName;    }}
四、MyOpenHelper(继承自SQLiteOpenHelper)
package com.example.mao.sqlite3demo;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;/** * Created by mao on 2017/8/25. */public class MyOpenHelper extends SQLiteOpenHelper {    /*    * 参数:1、上下文 2、数据库名 3、一般传null 4、数据库的版本号    * 当我们需要升级数据库的时候只需要提升数据库的版本号,比如23    * 这它会自动调用onUpgrade函数。    * */    public MyOpenHelper(Context context) {        super(context, "user_info.db", null, 1);    }    /*    * 该函数在第一次创建数据库时由系统自动调用    * SQLiteDatabase sqLiteDatabase参数为创建好的数据库    *    * */    @Override    public void onCreate(SQLiteDatabase sqLiteDatabase) {        //执行建表语句        sqLiteDatabase.execSQL("create table user(id integer primary key autoincrement,name text,age integer);");    }    /*    * 数据库更新的时候系统自动调用,可以在该方法执行一些更新操作    * */    @Override    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {    }}

)
原创粉丝点击