布局文件main实现简单的功能:

 1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:layout_width="fill_parent"
4 android:layout_height="fill_parent"
5 android:orientation="vertical" >
6
7 <TextView
8 android:id="@+id/showsomething"
9 android:layout_width="fill_parent"
10 android:layout_height="wrap_content"
11 android:text="@string/hello" />
12
13 <Button
14 android:id="@+id/btn_create"
15 android:layout_width="wrap_content"
16 android:layout_height="wrap_content"
17 android:text="创建数据库" />
18 <TextView
19 android:layout_width="wrap_content"
20 android:layout_height="wrap_content"
21 android:text="用户名:"
22 />
23 <EditText
24 android:id="@+id/username"
25 android:layout_width="fill_parent"
26 android:layout_height="40dp"
27 />
28 <TextView
29 android:layout_width="wrap_content"
30 android:layout_height="wrap_content"
31 android:text="密码:"
32 />
33 <EditText
34 android:id="@+id/password"
35 android:layout_width="fill_parent"
36 android:layout_height="40dp"
37 />
38 <Button
39 android:id="@+id/btn_insert"
40 android:layout_width="wrap_content"
41 android:layout_height="wrap_content"
42 android:text="添加用户"
43 android:textSize="20sp"
44 />
45
46 <Button
47 android:id="@+id/btn_update"
48 android:layout_width="wrap_content"
49 android:layout_height="wrap_content"
50 android:text="更新"
51 android:textSize="20sp"
52 />
53
54 <Button
55 android:id="@+id/btn_show"
56 android:layout_width="wrap_content"
57 android:layout_height="wrap_content"
58 android:text="显示用户"
59 android:textSize="20sp"
60 />
61
62 <Button
63 android:id="@+id/btn_showall"
64 android:layout_width="wrap_content"
65 android:layout_height="wrap_content"
66 android:text="显示all用户"
67 android:textSize="20sp"
68 />
69 <Button
70 android:id="@+id/btn_deleteusertable"
71 android:layout_width="wrap_content"
72 android:layout_height="wrap_content"
73 android:text="删除用户表"
74 android:textSize="20sp"
75 />
76 </LinearLayout>

工具类DBUtil.java是实现数据库的创建连接、断接、增删改查等操作。

package com.db.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Vector; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.EditText; public class DBUtil
{
static SQLiteDatabase sld;
public static void createOrOpenDatabase() throws Exception
{
sld=SQLiteDatabase.openDatabase
(
"/data/data/com.db/dbtest", //数据库所在路径
null, //CursorFactory
SQLiteDatabase.OPEN_READWRITE|SQLiteDatabase.CREATE_IF_NECESSARY //读写、若不存在则创建
);
String sql0="create table if not exists user(username varchar2(20),password varchar2(20))";
sld.execSQL(sql0);
} public static void closeDatabase() throws Exception
{
try
{
sld.close();
}
catch(Exception e)
{
e.printStackTrace();
}
} /*=====================================begin==========================================================*/
//获取用户信息-winxiang
public static List<String> searchuser(String username){
List<String> list=new ArrayList<String>();
try
{
createOrOpenDatabase();
String sql="select * from user where username='"+username+"'";
Cursor cur=sld.rawQuery(sql, new String[]{});
while(cur.moveToNext())
{
list.add(cur.getString(0)); //username
list.add(cur.getString(1)); //password
}
cur.close();
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
}
//获取所有用户信息-winxiang
public static List<String> searchalluser(){
List<String> list=new ArrayList<String>();
try
{
createOrOpenDatabase();
String sql="select * from user";
Cursor cur=sld.rawQuery(sql, new String[]{});
while(cur.moveToNext())
{
list.add(cur.getString(0)); //username
list.add(cur.getString(1)); //password
}
cur.close();
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
return list;
} public static void updatetable(String sql)
{
try
{
createOrOpenDatabase();
sld.execSQL(sql);
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
} //舍弃user表
public static void droptable(){
try
{
String sql="drop table user";
createOrOpenDatabase();
sld.execSQL(sql);
closeDatabase();
}
catch(Exception e)
{
e.printStackTrace();
}
Log.d("DB","had deleted table: user->");
}
/*=====================================end==========================================================*/
}

DBTestactivity:

 1 package com.db;
2
3
4 import java.util.List;
5
6 import com.db.util.DBUtil;
7 import android.app.Activity;
8 import android.os.Bundle;
9 import android.view.View;
10 import android.view.View.OnClickListener;
11 import android.widget.Button;
12 import android.widget.EditText;
13 import android.widget.TextView;
14 import android.widget.Toast;
15
16 public class DBtestActivity extends Activity {
17 Button btn_createdb,btn_insert,btn_show,btn_update,btn_showall,deleteusertable;
18 EditText username,password;
19 TextView showsomething;
20
21 @Override
22 public void onCreate(Bundle savedInstanceState) {
23 super.onCreate(savedInstanceState);
24 setContentView(R.layout.main);
25 init();
26 }
27
28 public void init(){
29 username = (EditText) findViewById(R.id.username);
30 password = (EditText) findViewById(R.id.password);
31 btn_createdb = (Button) findViewById(R.id.btn_create);
32 btn_insert = (Button) findViewById(R.id.btn_insert);
33 btn_show = (Button) findViewById(R.id.btn_show);
34 btn_update = (Button) findViewById(R.id.btn_update);
35 deleteusertable = (Button) findViewById(R.id.btn_deleteusertable);
36 btn_showall= (Button) findViewById(R.id.btn_showall);
37 showsomething = (TextView) findViewById(R.id.showsomething);
38
39 btn_createdb.setOnClickListener(listener);
40 btn_insert.setOnClickListener(listener);
41 btn_createdb.setOnClickListener(listener);
42 btn_show.setOnClickListener(listener);
43 btn_update.setOnClickListener(listener);
44 btn_showall.setOnClickListener(listener);
45 deleteusertable.setOnClickListener(listener);
46 }
47
48 public OnClickListener listener = new OnClickListener() {
49 @Override
50 public void onClick(View v) {
51 Button button = (Button) v;
52 if(button.getId()==btn_createdb.getId()){
53 try {
54 DBUtil.createOrOpenDatabase();
55 } catch (Exception e) {
56 e.printStackTrace();
57 }
58 }else if(button.getId()==btn_insert.getId()){
59 String sql="insert into user values ('"+username.getText()+"','"+password.getText()+"')";
60 DBUtil.updatetable(sql);
61 }else if(button.getId()==btn_show.getId()){
62 List<String>user = DBUtil.searchuser(username.getText().toString());
63 showsomething.setText(user.toString());
64 }else if(button.getId()==btn_update.getId()){
65 String sql="update user set username='"+username.getText()+"',password='"+password.getText()+"' where username = '"+username.getText()+"'";
66 System.out.println(sql);
67 DBUtil.updatetable(sql);
68 }else if(button.getId()==btn_showall.getId()){
69 List<String>users = DBUtil.searchalluser();
70 showsomething.setText(users.toString());
71 }else if(button.getId()==deleteusertable.getId()){
72 DBUtil.droptable();
73 Toast.makeText(getApplicationContext(), "用户表删除成功", Toast.LENGTH_SHORT).show();
74 }
75 }
76 };
77 }

靠谱好用,ANDROID SQLITE 增删查改的更多相关文章

  1. python下sqlite增删查改方法(转)

    sqlite读写   #coding=utf-8 import sqlite3 import os #创建数据库和游标 if os.path.exists(' test.db'): conn=sqli ...

  2. SQLite在Android程序中的使用方法,SQLite的增删查改方法

    Sqlite: 1.一款用来实现本地数据存储的轻量级数据管理工具,是众多用来实现数据库管理的工具之一. 2.Android已经将SQLite的代码功能吸收在它的系统中,我们可以直接在Android程序 ...

  3. Android SQLite最简单demo实现(增删查改)

    本来不太想写这篇博客的,但是看到网上的关于android数据库操作的博文都讲得很详细,对于像我这样的新手入门了解SQLite的基本操作有一定难度,所以我参考了网上的一些博客文章,并自己亲自摸索了一遍, ...

  4. Android——另外一种增删查改的方式(ContentProvider常用)

    以下介绍另外一种增删查改的方式 package com.njupt.sqllist; import java.util.ArrayList; import java.util.List; import ...

  5. C# SQLite 创建数据库的方法增删查改语法和命令

    SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...

  6. 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)

    1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...

  7. 后端Spring Boot+前端Android交互+MySQL增删查改

    2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...

  8. IOS CoreData的(增删查改)

    (1).CoreDataa>什么是CoreDatab>CoreData增删改查 "什么时候使用COredata 什么时候使用FMDatabases"CoreData 在 ...

  9. [置顶] cocos2dx sqllite 增删查改等操作

    首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会 ...

随机推荐

  1. Python小代码_5_二维矩阵转置

    使用列表推导式实现二维矩阵转置 matrix = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]] print(matrix) matrix_t = [[ro ...

  2. Bootstrap 遮罩层实现方式

    直接上代码: <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <me ...

  3. Node.js 模块

    稳定性: 5 - 锁定 Node 有简单的模块加载系统.在 Node 里,文件和模块是一一对应的.下面例子里,foo.js 加载同一个文件夹里的 circle.js 模块. foo.js 内容: va ...

  4. [OpenCV] How to install opencv by compiling source code

    Introduction Install OpenCV and its dependence ! STEPs 1, compiler sudo apt-get install build-essent ...

  5. 一道有趣的Twitter技术面试题

    来自:http://blog.jobbole.com/50705/ 看下面这个图片” “在这个图片里我们有不同高度的墙.这个图片由一个整数数组所代表,数组中每个数是墙的高度.上边的图可以表示为数组[2 ...

  6. PLSQL实现分页查询

    --集合实现游标查询 CREATE OR REPLACE PACKAGE emppkg IS TYPE t_record IS RECORD( rn INT, empno emp.empno%TYPE ...

  7. 剑指offer-面试题7:俩个栈实现队列(java)

    详细分析请参照C语言版,这里仅仅给出实现代码,注释很详细,不得不说java各种api用起来真是爽飞了 1 package com.xsf.SordForOffer; 2 3 import java.u ...

  8. Apache DbUtils 探秘

    听说Apache的DbUtils很好用,而且是对jdbc的简单的封装,所以可以和jdbc一起混搭,多以今天就来尝试一下,关于DbUtils 是如何使用的. 准备 数据库: MySQL 依赖: mysq ...

  9. java创建对象详解和多态问题

    一. java 构造方法不等于创建对象而是初始化对象,new 关键字分配内存和创建对象的.  二.Test test = new Test(); 有人用上面的表达式来说明构造方法返回对象引用,这是明显 ...

  10. 18 UI美化之level(等级显示显示)

    根据level显示哪张图片 在工程文件的res/drawable/新建level-list 如下 <?xml version="1.0" encoding="utf ...