Android中SQLiteDatabase的使用

2024-03-05 16:38

本文主要是介绍Android中SQLiteDatabase的使用,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接http://aina-hk55hk.iteye.com/blog/698794
package com.Aina.Android;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Test extends Activity {
/** Called when the activity is first created. */
private ListView lv = null;
private SQLiteDatabase mSQLiteDatabase = null;
private static final String DATABASE_NAME = "Test.db";
private static final String TABLE_NAME = "table_test";
private static final String COLUMN_ID = "_id";// INTEGER PRIMARY KEY
private static final String COLUMN_NAME = "name";// TEXT
private static final String COLUMN_AGE = "age";// INTEGER
private static final String CREATE_TABLE = "CREATE TABLE IF NOT EXISTS "
+ TABLE_NAME + " (" + COLUMN_ID + " INTEGER PRIMARY KEY,"
+ COLUMN_NAME + " TEXT," + COLUMN_AGE + " INTEGER)";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv = (ListView) this.findViewById(R.id.ListView01);
try {
mSQLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME,
Activity.MODE_PRIVATE, null);
} catch (Exception ex) {
this.ShowDialog("打开或者创建数据库异常:" + ex.getMessage());
}
try {
mSQLiteDatabase.execSQL(CREATE_TABLE);
} catch (Exception ex) {
this.ShowDialog("创建表异常:" + ex.getMessage());
}
// this.InsertData();
// this.InsertData2();
// this.AddData();
// this.UpdateData();
// this.UpdateData2();
// this.UpdateData3();
// this.DeleteData();
// this.DeleteData2();
// this.DeleteData3();
this.SelectData();
}

@Override
protected void onPause() {
super.onPause();
mSQLiteDatabase.close();// 关闭数据库
}

/**
* 插入数据-execSQL
*/
private void InsertData() {
try {
String str = "INSERT INTO " + TABLE_NAME + " (" + COLUMN_NAME + ","
+ COLUMN_AGE + ")VALUES('张三',30)";
mSQLiteDatabase.execSQL(str);
} catch (Exception ex) {
this.ShowDialog("插入数据异常:" + ex.getMessage());
}

}

/**
* 插入数据-execSQL
*/
private void InsertData2() {
try {
String str = "INSERT INTO " + TABLE_NAME + " (" + COLUMN_NAME + ","
+ COLUMN_AGE + ")VALUES(?,?)";
Object[] ob = new Object[] { "王五", 50 };
mSQLiteDatabase.execSQL(str, ob);
} catch (Exception ex) {
this.ShowDialog("插入数据异常:" + ex.getMessage());
}
}

/**
* 插入数据-insert() nullColumnHack,这个参数需要传入一个列名。SQL标准并不允许插入所有列均为空的一行数据,
* 所以当传入的initialValues值为空或者为0时
* ,用nullColumnHack参数指定的列会被插入值为NULL的数据,然后再将此行插入到表中。
*/
private void AddData() {
try {
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME, "李四");
cv.put(COLUMN_AGE, 40);
// long num = mSQLiteDatabase.insert(TABLE_NAME, COLUMN_NAME, cv);
long num = mSQLiteDatabase.insertOrThrow(TABLE_NAME, null, cv);
this.setTitle("num==" + num);
} catch (Exception ex) {
this.ShowDialog("插入数据异常:" + ex.getMessage());
}

}

/**
* 更新数据
*/
private void UpdateData() {
try {
String str = "UPDATE " + TABLE_NAME + " SET " + COLUMN_AGE
+ " = 25 WHERE _id=1";
mSQLiteDatabase.execSQL(str);
} catch (Exception ex) {
this.ShowDialog("更新数据异常:" + ex.getMessage());
}
}

/**
* 更新数据
*/
private void UpdateData2() {
try {
String str = "UPDATE " + TABLE_NAME + " SET " + COLUMN_AGE
+ " = ? WHERE _id=?";
Object[] Ob = new Object[] { 33, 2 };
mSQLiteDatabase.execSQL(str, Ob);
} catch (Exception ex) {
this.ShowDialog("更新数据异常:" + ex.getMessage());
}
}

/**
* 更新数据
*/
private void UpdateData3() {
try {
ContentValues cv = new ContentValues();
cv.put(COLUMN_NAME, "李四4");
cv.put(COLUMN_AGE, 43);
int num = mSQLiteDatabase.update(TABLE_NAME, cv,
COLUMN_NAME + "=?", new String[] { "李四" });
this.setTitle("修改行数num=" + num);
} catch (Exception ex) {
this.ShowDialog("更新数据异常:" + ex.getMessage());
}
}

/**
* 删除数据
*/
private void DeleteData() {
try {
String str = "DELETE FROM " + TABLE_NAME + " WHERE _id=3";
mSQLiteDatabase.execSQL(str);
} catch (Exception ex) {
this.ShowDialog("删除数据异常:" + ex.getMessage());
}
}

/**
* 删除数据
*/
private void DeleteData2() {
try {
String str = "DELETE FROM " + TABLE_NAME + " WHERE _id=?";
mSQLiteDatabase.execSQL(str, new Object[] { 2 });
} catch (Exception ex) {
this.ShowDialog("删除数据异常:" + ex.getMessage());
}
}

/**
* 删除数据
*/
private void DeleteData3() {
try {
int num = mSQLiteDatabase.delete(TABLE_NAME, "_id=1", null);
this.setTitle("删除行数num=" + num);
} catch (Exception ex) {
this.ShowDialog("删除数据异常:" + ex.getMessage());
}
}

/**
* 查询数据
*/
private void SelectData() {
try {
String sql = "SELECT * FROM " + TABLE_NAME;
Cursor cursor = mSQLiteDatabase.rawQuery(sql,null);
// Cursor cursor = mSQLiteDatabase.query(TABLE_NAME, new String[] {
// COLUMN_ID, COLUMN_NAME, COLUMN_AGE }, COLUMN_NAME + "=?",
// new String[] { "李四" }, null, null, null);
if (cursor != null) {
ListAdapter adapter = new SimpleCursorAdapter(this,
R.layout.ss, cursor, new String[] { COLUMN_ID,
COLUMN_NAME, COLUMN_AGE },
new int[] { R.id.TextView1, R.id.TextView2,
R.id.TextView3 });
lv.setAdapter(adapter);
}
} catch (Exception ex) {
this.ShowDialog("查询数据异常:" + ex.getMessage());
}
}
/**
* 删除表
*/
private void DropTable(){
try{
String sql = "DROP TABLE "+TABLE_NAME;
mSQLiteDatabase.execSQL(sql);
}catch(Exception ex){
this.ShowDialog("删除表异常:"+ex.getMessage());
}
}
/**
* 删除数据库
*/
private void DropDatabase(){
try{
this.deleteDatabase(DATABASE_NAME);
}catch(Exception ex){
this.ShowDialog("删除数据库异常:"+ex.getMessage());
}
}
/**
* 提示对话框
* @param msg
*/
private void ShowDialog(String msg) {
new AlertDialog.Builder(this).setTitle("提示").setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}

}).show();
}
}
2.main.xml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:id="@+id/ListView01" android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/hello" />
<ListView android:id="@+id/ListView01" android:layout_width="fill_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>


3.ss.xml
Java代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:paddingTop="5dip" android:paddingBottom="5dip" android:paddingLeft="5dip"
android:layout_height="wrap_content">
<TextView android:id="@+id/TextView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
<TextView android:id="@+id/TextView2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20px" android:layout_marginLeft="50dip" android:paddingRight="50dip"
android:text="" />
<TextView android:id="@+id/TextView3" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />

这篇关于Android中SQLiteDatabase的使用的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777136

相关文章

使用Python删除Excel中的行列和单元格示例详解

《使用Python删除Excel中的行列和单元格示例详解》在处理Excel数据时,删除不需要的行、列或单元格是一项常见且必要的操作,本文将使用Python脚本实现对Excel表格的高效自动化处理,感兴... 目录开发环境准备使用 python 删除 Excphpel 表格中的行删除特定行删除空白行删除含指定

深入理解Go语言中二维切片的使用

《深入理解Go语言中二维切片的使用》本文深入讲解了Go语言中二维切片的概念与应用,用于表示矩阵、表格等二维数据结构,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习吧... 目录引言二维切片的基本概念定义创建二维切片二维切片的操作访问元素修改元素遍历二维切片二维切片的动态调整追加行动态

prometheus如何使用pushgateway监控网路丢包

《prometheus如何使用pushgateway监控网路丢包》:本文主要介绍prometheus如何使用pushgateway监控网路丢包问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录监控网路丢包脚本数据图表总结监控网路丢包脚本[root@gtcq-gt-monitor-prome

Python通用唯一标识符模块uuid使用案例详解

《Python通用唯一标识符模块uuid使用案例详解》Pythonuuid模块用于生成128位全局唯一标识符,支持UUID1-5版本,适用于分布式系统、数据库主键等场景,需注意隐私、碰撞概率及存储优... 目录简介核心功能1. UUID版本2. UUID属性3. 命名空间使用场景1. 生成唯一标识符2. 数

SpringBoot中如何使用Assert进行断言校验

《SpringBoot中如何使用Assert进行断言校验》Java提供了内置的assert机制,而Spring框架也提供了更强大的Assert工具类来帮助开发者进行参数校验和状态检查,下... 目录前言一、Java 原生assert简介1.1 使用方式1.2 示例代码1.3 优缺点分析二、Spring Fr

Android kotlin中 Channel 和 Flow 的区别和选择使用场景分析

《Androidkotlin中Channel和Flow的区别和选择使用场景分析》Kotlin协程中,Flow是冷数据流,按需触发,适合响应式数据处理;Channel是热数据流,持续发送,支持... 目录一、基本概念界定FlowChannel二、核心特性对比数据生产触发条件生产与消费的关系背压处理机制生命周期

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

Android ClassLoader加载机制详解

《AndroidClassLoader加载机制详解》Android的ClassLoader负责加载.dex文件,基于双亲委派模型,支持热修复和插件化,需注意类冲突、内存泄漏和兼容性问题,本文给大家介... 目录一、ClassLoader概述1.1 类加载的基本概念1.2 android与Java Class

SpringBoot线程池配置使用示例详解

《SpringBoot线程池配置使用示例详解》SpringBoot集成@Async注解,支持线程池参数配置(核心数、队列容量、拒绝策略等)及生命周期管理,结合监控与任务装饰器,提升异步处理效率与系统... 目录一、核心特性二、添加依赖三、参数详解四、配置线程池五、应用实践代码说明拒绝策略(Rejected

C++ Log4cpp跨平台日志库的使用小结

《C++Log4cpp跨平台日志库的使用小结》Log4cpp是c++类库,本文详细介绍了C++日志库log4cpp的使用方法,及设置日志输出格式和优先级,具有一定的参考价值,感兴趣的可以了解一下... 目录一、介绍1. log4cpp的日志方式2.设置日志输出的格式3. 设置日志的输出优先级二、Window