我是靠谱客的博主 震动哈密瓜,最近开发中收集的这篇文章主要介绍Android开发使用SQLite数据库1、总体思路2、实现代码3、测试代码4、演示效果,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
1、总体思路
1、自定义一个类MySqliteOpenHelper
继承SQLiteOpenHelper
类。
2、实现自定义类的单例模式。
3、在onCreate
抽象方法中创建表。
4、通过单例模式获取对象实现增、删、改、查。
2、实现代码
a、自定义类实现单例模式
package com.example.learning01;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
/**
*
* 单例模式
*
*/
public class MySqliteOpenHelper extends SQLiteOpenHelper {
private static SQLiteOpenHelper mInstance;
public static synchronized SQLiteOpenHelper getmInstance(Context context) {
if(mInstance == null) {
mInstance = new MySqliteOpenHelper(context,"xupeng.dp",null,1);
}
return mInstance;
}
//将构造方法私有化
private MySqliteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建表 表数据初始化
//id的标准写法:_id
//主键必须为1
String sql = "create table persons(_id integer primary key autoincrement, name text)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
b、增
public void insert_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen()){
String sql = "insert into persons(name) values('xdupeng01')";
db.execSQL(sql);
}
db.close();//关闭数据库
}
c、删
public void delete_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen()){
String sql = "delete from persons where _id=?";
db.execSQL(sql,new Object[]{4});
}
db.close();//关闭数据库
}
d、改
public void update_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen()){
String sql = "update persons set name=? where _id=?";
db.execSQL(sql,new Object[]{"李连杰",5});
}
db.close();//关闭数据库
}
e、查
public void find_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getReadableDatabase();
if(db.isOpen()) {
//返回游标
Cursor cursor = db.rawQuery("select * from persons", null);
while (cursor.moveToNext()) {
@SuppressLint("Range") int _id = cursor.getInt(cursor.getColumnIndex("_id"));
@SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
Log.d(TAG, "id: "+_id + " name :"+name);
}
cursor.close();//关闭游标
db.close();//关闭数据库
}
}
3、测试代码
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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:text="生成数据库"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="create_db"/>
<Button
android:text="查询"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="find_db"/>
<Button
android:text="插入"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="insert_db"/>
<Button
android:text="修改"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="update_db"/>
<Button
android:text="删除"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="delete_db"/>
</LinearLayout>
MySqliteOpenHelper.java
package com.example.learning01;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
/**
*
* 单例模式
*
*/
public class MySqliteOpenHelper extends SQLiteOpenHelper {
private static SQLiteOpenHelper mInstance;
public static synchronized SQLiteOpenHelper getmInstance(Context context) {
if(mInstance == null) {
mInstance = new MySqliteOpenHelper(context,"xupeng.dp",null,1);
}
return mInstance;
}
private MySqliteOpenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//创建表 表数据初始化
//id的标准写法
//主键必须为1
String sql = "create table persons(_id integer primary key autoincrement, name text)";
sqLiteDatabase.execSQL(sql);
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MainActivity.java
package com.example.learning01;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void create_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
//文件夹的创建靠下面这句话
SQLiteDatabase read = helper.getReadableDatabase();
}
public void find_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getReadableDatabase();
if(db.isOpen()) {
//返回游标
Cursor cursor = db.rawQuery("select * from persons", null);
while (cursor.moveToNext()) {
@SuppressLint("Range") int _id = cursor.getInt(cursor.getColumnIndex("_id"));
@SuppressLint("Range") String name = cursor.getString(cursor.getColumnIndex("name"));
Log.d(TAG, "id: "+_id + " name :"+name);
}
cursor.close();//关闭游标
db.close();//关闭数据库
}
}
public void insert_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen()){
String sql = "insert into persons(name) values('xdupeng01')";
db.execSQL(sql);
}
db.close();//关闭数据库
}
public void update_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen()){
String sql = "update persons set name=? where _id=?";
db.execSQL(sql,new Object[]{"李连杰",5});
}
db.close();//关闭数据库
}
public void delete_db(View view) {
SQLiteOpenHelper helper = MySqliteOpenHelper.getmInstance(this);
SQLiteDatabase db = helper.getWritableDatabase();
if(db.isOpen()){
String sql = "delete from persons where _id=?";
db.execSQL(sql,new Object[]{4});
}
db.close();//关闭数据库
}
}
4、演示效果
最后
以上就是震动哈密瓜为你收集整理的Android开发使用SQLite数据库1、总体思路2、实现代码3、测试代码4、演示效果的全部内容,希望文章能够帮你解决Android开发使用SQLite数据库1、总体思路2、实现代码3、测试代码4、演示效果所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复