靠谱好用,ANDROID SQLITE 增删查改
布局文件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 增删查改的更多相关文章
- python下sqlite增删查改方法(转)
sqlite读写 #coding=utf-8 import sqlite3 import os #创建数据库和游标 if os.path.exists(' test.db'): conn=sqli ...
- SQLite在Android程序中的使用方法,SQLite的增删查改方法
Sqlite: 1.一款用来实现本地数据存储的轻量级数据管理工具,是众多用来实现数据库管理的工具之一. 2.Android已经将SQLite的代码功能吸收在它的系统中,我们可以直接在Android程序 ...
- Android SQLite最简单demo实现(增删查改)
本来不太想写这篇博客的,但是看到网上的关于android数据库操作的博文都讲得很详细,对于像我这样的新手入门了解SQLite的基本操作有一定难度,所以我参考了网上的一些博客文章,并自己亲自摸索了一遍, ...
- Android——另外一种增删查改的方式(ContentProvider常用)
以下介绍另外一种增删查改的方式 package com.njupt.sqllist; import java.util.ArrayList; import java.util.List; import ...
- C# SQLite 创建数据库的方法增删查改语法和命令
SQLite介绍 SQLite是一个开源.免费的小型RDBMS(关系型数据库),能独立运行.无服务器.零配置.支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准. SQLite数据库官方主页 ...
- 后端Spring Boot+前端Android交互+MySQL增删查改(Java+Kotlin实现)
1 前言&概述 这篇文章是基于这篇文章的更新,主要是更新了一些技术栈以及开发工具的版本,还有修复了一些Bug. 本文是SpringBoot+Android+MySQL的增删查改的简单实现,用到 ...
- 后端Spring Boot+前端Android交互+MySQL增删查改
2021.1.27 更新 已更新新版本博客,更新内容很多,因此新开了一篇博客,戳这里. 1 概述 使用spring boot作为后端框架与Android端配合mysql进行基本的交互,包含了最基本的增 ...
- IOS CoreData的(增删查改)
(1).CoreDataa>什么是CoreDatab>CoreData增删改查 "什么时候使用COredata 什么时候使用FMDatabases"CoreData 在 ...
- [置顶] cocos2dx sqllite 增删查改等操作
首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会 ...
随机推荐
- DDD实战进阶第一波(八):开发一般业务的大健康行业直销系统(业务逻辑条件判断最佳实践)
这篇文章其实是大健康行业直销系统的番外篇,主要给大家讲讲如何在领域逻辑中,有效的处理业务逻辑条件判断的最佳实践问题. 大家都知道,聚合根.实体和值对象这些领域对象都自身处理自己的业务逻辑.在业务处理过 ...
- Emacs Python 自动补全--Elpy
安装方法: 首先,安装一些依赖包: # Either of these pip install rope pip install jedi # flake8 用来检查语法错误 pip install ...
- RedHatEnterpriseLinuxServerRelease7.3上配置vsftp服务器
1.vsftpd 服务启停相关命令 systemctl start vsftpd systemctl stop vsftpd systemctl restart vsftpd 2.配置文件/etc/v ...
- MySQL数据库优化的八种方式
引言: 关于数据库优化,网上有不少资料和方法,但是不少质量参差不齐,有些总结的不够到位,内容冗杂 偶尔发现了这篇文章,总结得很经典,文章流量也很大,所以拿到自己的总结文集中,积累优质文章,提升个人能力 ...
- Node.js Net 模块
Node.js Net 模块提供了一些用于底层的网络通信的小工具,包含了创建服务器/客户端的方法,我们可以通过以下方式引入该模块: var net = require("net") ...
- HTML DOM 改变 HTML 内容
HTML DOM 允许 JavaScript 改变 HTML 元素的内容. 改变 HTML 输出流 JavaScript 能够创建动态的 HTML 内容: 今天的日期是: Thu Feb 25 201 ...
- Mac 下 查看 使用某端口的进程和关闭该进程的命令
查看使用某端口的进程 最简单的命令是: lsof -i :端口号 如果要使用管理员权限那么就是: sudo lsof -i :端口号 所以查看 使用某端口号3000的进程可以使用: lsof -i : ...
- DOS界面下的翻译软件制作
准备 素材 依赖 接口 地址 参数 返回值解析 编码及测试 功能代码 运行脚本 环境变量 结果展示 英语转汉语 汉语转英语 总结 昨天看到一篇关于Linux下的桌面词典的文章,于是就想实现一个Wind ...
- Xcode7.3中SKAudioNode"诡异"初始化的解决
大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 我没有在之前版本的Xcode中测试,不过很多人反映SKAudi ...
- CUSTOM.PLL的使用
在开发中对系统标准form的修改一般不建议修改系统原有FORM,对所需要修改的内容一般写在CUSTOM.PLL里即可,应为每个form运行的时候都会调用CUSTOM.PLL具体概念性东西可参考网上资料 ...