sqlite是一个小型的数据库,占用资源低,处理速度快等优点。
首先新建一个MysqLiteOpenHelper类然后继承sqliteOpenHelper。新建一个构造方法,里面有四个参数,第二个是数据库的名字,可命名为xx.db,第三个factory 游标工厂 用来创建一个游标默认值是null,第四个参数是版本号,最低为1.onCreate只有第一次创建的时候才会执行,onUpgrade是版本更新的时候才会调用。
db.execsql("create table student1(_id integer primary key autoincrement,name varchar(20),gender varchar(30))");这里是创建一个student1 的数据库,有id、key、name、gender,这个可根据自己需求定义。
下面是完整代码
package com.example.apple.sqlitedemo; import android.content.Context; import android.database.sqlite.sqliteDatabase; import android.database.sqlite.sqliteOpenHelper; import android.util.Log; /** * Created by apple on 16/12/28. */ public class MysqLiteOpenHelper extends sqliteOpenHelper{ private final String TAG = "MysqLiteOpenHelper"; public MysqLiteOpenHelper(Context context) { //name数据库名字, //factory 游标工厂 用来创建一个游标默认值是null //version 版本号 最低1 super(context,"student.db",null,4); } /** * 只有第一次创建的时候才调用执行 * @param db */ @Override public void onCreate(sqliteDatabase db) { Log.e(TAG,"onCreate"); db.execsql("create table student1(_id integer primary key autoincrement,gender varchar(30))"); } /** * 数据库版本更新时调用 * @param db * @param oldVersion * @param newVersion */ @Override public void onUpgrade(sqliteDatabase db,int oldVersion,int newVersion) { Log.e(TAG,"onUpgrade"); } }
2、新建一个bean类用于存储数据,这里定义了就name 和gen der
package com.example.apple.sqlitedemo.bean; /** * Created by apple on 16/12/28. */ public class StudentData { private String name; private String gender; public StudentData(String name,String gender) { this.name = name; this.gender = gender; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }3、新建一个StudentDao的类用于实现增删改查
首先创建一个构造方法,在构造方法中初始化sqliteOpenHelper,用于新建一个数据库,具体实现方法可看代码,注释得很详细了
package com.example.apple.sqlitedemo.dao; import android.content.Context; import android.database.Cursor; import android.database.sqlite.sqliteDatabase; import android.util.Log; import com.example.apple.sqlitedemo.MysqLiteOpenHelper; import com.example.apple.sqlitedemo.bean.StudentData; import java.util.ArrayList; import java.util.List; /** * Created by apple on 16/12/28. */ public class StudentDao { public MysqLiteOpenHelper MysqLiteOpenHelper; private final String TAG = "StudentDao"; //创建构造方法,在构造方法中初始化sqliteOpenHelper public StudentDao(Context context) { MysqLiteOpenHelper = new MysqLiteOpenHelper(context); } /** * 往数据库中添加数据 * @param name * @param gender */ public void add(String name,String gender){ sqliteDatabase db = MysqLiteOpenHelper.getWritableDatabase(); //? 为占位符 db.execsql("insert into student1(name,gender) values(?,?)",new Object[]{name,gender}); db.close();//关闭数据库 } /** * 删除数据 * @param name */ public void delete(String name){ sqliteDatabase db = MysqLiteOpenHelper.getWritableDatabase(); db.execsql("delete from student1 where name = ?",new Object[]{name}); db.close();//关闭数据库 } /** * 更新数据库数据 * @param name * @param gender */ public void update(String name,String gender){ sqliteDatabase db = MysqLiteOpenHelper.getWritableDatabase(); db.execsql("update student1 set gender = ? where name = ?",new Object[]{gender,name}); db.close();//关闭数据库 } /** * 查询数据库数据 * @return */ public List<StudentData> query(){ List<StudentData> studentDatas = new ArrayList<>(); sqliteDatabase db = MysqLiteOpenHelper.getWritableDatabase(); //通过rawQuery 查询拿到一个cursor Cursor cursor = db.rawQuery("select * from student1",new String[]{}); //通过cursor获取所在的索引值 while (cursor.moveToNext()) { String name = cursor.getString(cursor.getColumnIndex("name")); String gender = cursor.getString(cursor.getColumnIndex("gender")); studentDatas.add(new StudentData(name,gender)); Log.e(TAG,"name="+name+" gender="+gender); } return studentDatas; } }4、最后引用测试是否可以实现功能,先写一个测试布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/input_data" android:layout_width="match_parent" android:layout_height="wrap_content" /> <Button android:id="@+id/add" android:layout_marginTop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="添加" /> <Button android:id="@+id/delete" android:layout_marginTop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除" /> <Button android:id="@+id/update" android:layout_marginTop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改" /> <Button android:id="@+id/query" android:layout_marginTop="10dip" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查找" /> </LinearLayout>初始化StudentDao后就可以调用里面的方法测试,具体代码
package com.example.apple.sqlitedemo; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import com.example.apple.sqlitedemo.bean.StudentData; import com.example.apple.sqlitedemo.dao.StudentDao; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener{ //private MysqLiteOpenHelper helper; private Button add; private Button delete; private Button update; private Button query; private EditText inputData; private final String TAG = "MainActivity"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { /** * name 数据库名字 * mode 权限模式 * factroy 游标工厂 */ // sqliteDatabase db = this.openOrCreateDatabase("ning",MODE_PRIVATE,null); // db.execsql("create table student(_id integer primary key autoincrement,name varchar(30),sex varchar(30))"); // db.execsql("insert into student (name,sex) values(?,new Object[18]); //实例化sqliteOpenHelper对象,这个时候数据库还没创建出来 // helper = new MysqLiteOpenHelper(this); //创建一个可读可写数据库 // sqliteDatabase writeDatabase = helper.getWritableDatabase(); //创建一个只可读的数据库 //sqliteDatabase readDatabase = helper.getReadableDatabase(); add = (Button)findViewById(R.id.add); add.setOnClickListener(this); delete = (Button)findViewById(R.id.delete); delete.setOnClickListener(this); update = (Button)findViewById(R.id.update); update.setOnClickListener(this); query = (Button)findViewById(R.id.query); query.setOnClickListener(this); inputData = (EditText)findViewById(R.id.input_data); } @Override public void onClick(View v) { StudentDao dao = new StudentDao(this); switch (v.getId()){ case R.id.add: dao.add("张三","男"); dao.add("李四","男"); dao.add("小明","女"); break; case R.id.delete: dao.delete("小明"); break; case R.id.update: dao.update("李四","女"); break; case R.id.query: List<StudentData> query = dao.query(); for (StudentData studentData : query){ Log.e(TAG,"学生姓名="+studentData.getName()+ "\n学生性别="+studentData.getGender()); } break; default: break; } } }
最后代码下载地址是: http://download.csdn.net/detail/u011324501/9724044 原文链接:https://www.f2er.com/sqlite/198609.html