布局文件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. Why Helm? - 每天5分钟玩转 Docker 容器技术(160)

    本章我们将学习 Helm,Kubernetes 的包管理器. 每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Hel ...

  2. 判定程序员等级,HashMap就够了

    JDK1.8  HashMap源码分析 用到的符号: ^异运算:两个操作数相同,结果是;两个操作数不同,结果是1. &按位与:两个操作数都是1,结果才是1. 一.HashMap概述 在JDK1 ...

  3. leetcode刷题笔记326 3的幂

    题目描述: 给出一个整数,写一个函数来确定这个数是不是3的一个幂. 后续挑战:你能不使用循环或者递归完成本题吗? 题目分析: 既然不使用循环或者递归,那我可要抖机灵了 如果某个数n为3的幂 ,则k=l ...

  4. 聊聊jstack的工作原理

    实现一个jstack 在聊Jstack得工作原理前呢,不如让我们先写一个简单的jstack玩玩.不用怕,很简单的,就几行代码的事,看: public class MyJstack { public s ...

  5. Node.js 常用工具util

    util 是一个Node.js 核心模块,提供常用函数的集合,用于弥补核心JavaScript 的功能 过于精简的不足. util.inherits util.inherits(constructor ...

  6. SQL_CALC_FOUND_ROWS equivalent in PostgreSQL

    https://www.postgresql.org/message-id/1185863074.10580.91.camel%40linda.lfix.co.uk On Tue, 2007-07-3 ...

  7. android M Launcher之LauncherModel (三)

    通过前两篇的分析,我们已经知道了LauncherModel的初始化及工作流程,如果您还不熟悉的话请看前两篇博文 android M Launcher之LauncherModel (一) android ...

  8. No Team Selected:A team must be selected to run 'ProjectName' on iPhoneName

    1. 现象:发布在 app store 的 qzone,app bundle identifier:com.tencent.qzone证书支持com.tencent.*然后自已写的测试demo,bun ...

  9. Android自定义ViewGroup(四、打造自己的布局容器)

    转载请标明出处: http://blog.csdn.net/xmxkf/article/details/51500304 本文出自:[openXu的博客] 目录: 简单实现水平排列效果 自定义Layo ...

  10. android开发常用工具箱

    我的工具包资料目录 我的个人总结,最近做的项目需要了的一些资料,感觉挺乱的,然后现在整理了一下. Jar包 包名 版本号 作用 下载地址 xUtils 2.6.14和3.1.26 大文件上传下载等 旧 ...