靠谱好用,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中生成会 ...
随机推荐
- mysql常见的优化需要注意的点
1.explain分析explian引用索引基数show indexes from table_name;主键索引具有最好的基数 测试时 不走缓存SELECT SQL_NO_CACHE id from ...
- js判断奇偶数实现隐藏显示功能 与css立体按钮
hello! 好久不见了 ,今天也没准备什么技术,知识想和大家就见个面,一个js判断奇数偶数来实现css样式 ,感觉最大的用途就是页面的导航.就这么一个小小的技术. 劳动快乐 当!当!当! ...
- Python中迭代输出(index,value)的几种方法
需求如下:迭代输出序列的索引(index)和索引值(value). 1.创建测试列表: >>> lst = [1,2,3,4,5] 2.实现方法如下: #方法1:range()+le ...
- FJUT第四周寒假作业[JL]最后的晚餐(动态规划)
题目来源:http://210.34.193.66:8080/vj/Contest.jsp?cid=163#P4 [JL]最后的晚餐 TimeLimit:1000MS MemoryLimit:100 ...
- PHP 实例 AJAX 与 MySQL
AJAX 数据库实例 下面的实例将演示网页如何通过 AJAX 从数据库读取信息: 实例 Person info will be listed here... 实例解释 - MySQL 数据库 在上 ...
- Android Design Support Library使用详解——TextInputLayout与TextInputEditText
TextInputLayout 在谷歌的Material Design中,文本输入是这样表现的:当用户点击输入框想要输入文字时,如果输入框是空的,那么它的提示文字(hint)就会变小并且同时移动到输入 ...
- springMVC源码分析--ModelAndViewContainer和ModelMap
ModelAndViewContainer主要是用来返回Model对象的,在ModelAndViewContainer中有defaultModel和redirectModel, defaultMode ...
- O(1)空间内实现矩阵转置
思路: * 每个元素转置前后会形成一个环(一个数字有多个环) * 利用环来移动元素达到转置 * 关键: * 1.得到元素下标的前驱后继, * 2.判断环是否已走过(意味属于一个环的元素一次转 ...
- Bootstrap3 排版-内联文本元素
标记文本 突出显示的文本由于其相关性在另一个上下文中,使用<mark>标记. You can use the mark tag to highlight text. You can use ...
- Spark-SQL之DataFrame操作大全
Spark SQL中的DataFrame类似于一张关系型数据表.在关系型数据库中对单表或进行的查询操作,在DataFrame中都可以通过调用其API接口来实现.可以参考,Scala提供的DataFra ...