Android SQLite数据库简单使用

作者:袖梨 2022-11-14


一、原理

SQLite的相关介绍我就不说了,想了解的同学可以谷歌或百度一下,好~ 直接进入主题,要使用SQLite存储数据,首先先创建数据库,创建的方法如下:


SQLiteDatabase db = openOrCreateDatabase("数据库名字.db", Context.MODE_PRIVATE, null);
db.execSQL("DROP TABLE IF EXISTS person");
// 创建person表
db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)")当创建完数据库后就可以使用SQLite了,接下来我们对数据库进行 增、删、改、查操作l了。

增:

代码如下 复制代码

Person person = new Person();
person.setName("john");
person.setAge(19);
// 插入数据
db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)",new Object[] { person.getName(), person.getAge() });

删:

代码如下 复制代码
String whereClause = "_id=?";//删除的条件
String[] whereArgs = {"1"};//删除的条件参数
db.delete("person",whereClause,whereArgs);

//执行删除改:

代码如下 复制代码

Person person2 = new Person();
person2.setName("john");
person2.setAge(20);
ContentValues cv = new ContentValues();
cv.put("age", person2.getAge()); //修改内容
db.update("person", cv, "name = ?",new String[] { person2.getName() });

查:

代码如下 复制代码

Cursor c = db.rawQuery("select * from person where name=?",new String[]{"john"});
while(c.moveToNext()){
int id =Integer.parseInt(c.getString(c.getColumnIndex("_id")));
String name =c.getString(c.getColumnIndex("name"));
String age = c.getString(c.getColumnIndex("age"));
Log.v("Test ", "id:" + id + " name: " + name + " age: " + age);
}

下面是完整的代码。

二、代码

第一步:

代码如下 复制代码

xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >


android:id="@+id/btn_create_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="CreateDataBase"
/>

android:id="@+id/btn_add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_create_database"
android:text="Add"
/>

android:id="@+id/btn_update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_add"
android:text="Update"
/>
android:id="@+id/btn_query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_update"
android:text="Query"
/>
android:id="@+id/btn_delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/btn_query"
android:text="Delete"
/>


第二步:

代码如下 复制代码

package com.msquirrel.activity;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.example.demo_sqlite.R;
import com.msquirrel.model.Person;

/**
* 执行 创建数据库、增加数据、删除数据、修改数据、查询数据。
*
* @author msquirrel
*
*/
public class SQLiteActivity extends Activity {

private Button btnCteateDataBase = null;
private Button btnAdd = null;
private Button btnUpdate = null;
private Button btnQuery = null;
private Button btnDelete = null;

private SQLiteDatabase db = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sqlite);
// 打开或创建test.db数据库
db = openOrCreateDatabase("test.db", Context.MODE_PRIVATE, null);
initView();
setListener();
}

private void initView() {
// TODO Auto-generated method stub
btnCteateDataBase = (Button) findViewById(R.id.btn_create_database);
btnAdd = (Button) findViewById(R.id.btn_add);
btnUpdate = (Button) findViewById(R.id.btn_update);
btnQuery = (Button) findViewById(R.id.btn_query);
btnDelete = (Button) findViewById(R.id.btn_delete);
}

private void setListener() {
// TODO Auto-generated method stub
btnCteateDataBase.setOnClickListener(new myOnClickListener());
btnAdd.setOnClickListener(new myOnClickListener());
btnUpdate.setOnClickListener(new myOnClickListener());
btnQuery.setOnClickListener(new myOnClickListener());
btnDelete.setOnClickListener(new myOnClickListener());
}

private class myOnClickListener implements OnClickListener {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

switch (v.getId()) {
case R.id.btn_create_database:
db.execSQL("DROP TABLE IF EXISTS person");
// 创建person表
db.execSQL("CREATE TABLE person (_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, age SMALLINT)");
break;
case R.id.btn_add:
Person person = new Person();
person.setName("john");
person.setAge(19);
// 插入数据
db.execSQL("INSERT INTO person VALUES (NULL, ?, ?)",
new Object[] { person.getName(), person.getAge() });
break;
case R.id.btn_update:
Person person2 = new Person();
person2.setName("john");
person2.setAge(20);
ContentValues cv = new ContentValues();
cv.put("age", person2.getAge()); //修改内容
db.update("person", cv, "name = ?",
new String[] { person2.getName() });
break;
case R.id.btn_query:
Cursor c = db.rawQuery("select * from person where name=?",new String[]{"john"});
while(c.moveToNext()){
int id = Integer.parseInt(c.getString(c.getColumnIndex("_id")));
String name =c.getString(c.getColumnIndex("name"));
String age = c.getString(c.getColumnIndex("age"));
Log.v("Test ", "id:" + id + " name: " + name + " age: " + age);
}
break;
case R.id.btn_delete:
String whereClause = "_id=?";//删除的条件
String[] whereArgs = {"1"};//删除的条件参数
db.delete("person",whereClause,whereArgs);//执行删除

break;
default:
break;
}

}

}

}


第三步:

代码如下 复制代码


package com.msquirrel.model;
/**
* 实体类
* @author Msquirrel
*
*/
public class Person {

private int _id;
private String name;
private int age;

public int get_id() {
return _id;
}
public void set_id(int _id) {
this._id = _id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}

}

最后大家要记得把SQLiteDatabase给关掉。

相关文章

精彩推荐