SQLite的使用案例
示例图 :

activity_main.xml :
<TextView
android:id="@+id/t1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:text="Name:"/> <EditText
android:id="@+id/text_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Input Name"
android:layout_toRightOf="@+id/t1"
android:layout_marginLeft="100dp"
android:layout_marginRight="10dp"
/>
<TextView
android:id="@+id/t2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_below="@+id/t1"
android:textSize="20dp"/>
<EditText
android:id="@+id/text_password"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:hint="Input Password"
android:layout_below="@+id/text_name"
android:layout_toRightOf="@id/t2"
android:layout_marginLeft="65dp"
android:layout_marginRight="10dp"
/>
<Button
android:id="@+id/btn_reg"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t2"
android:layout_marginTop="30dp"
android:text="注册" /> <Button
android:id="@+id/btn_del"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t2"
android:layout_marginTop="30dp"
android:layout_toRightOf="@+id/btn_reg"
android:text="删除" />
<Button
android:id="@+id/btn_sel"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_below="@+id/t2"
android:layout_marginTop="30dp"
android:layout_toRightOf="@id/btn_del"
android:text="查询" />
<LinearLayout
android:id="@+id/layout"
android:layout_below="@id/btn_sel"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent"
>
</LinearLayout>
</RelativeLayout> MainActivity.java :
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
LinearLayout Layout ;
EditText Name,Password;
Button bt_reg,bt_del,bt_sel;
List<User> userList;
private MyDatabaseHelper dbhelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
Layout=(LinearLayout) findViewById(R.id.layout);
Name=(EditText)findViewById(R.id.text_name);
Password=(EditText)findViewById(R.id.text_password);
bt_reg=(Button)findViewById(R.id.btn_reg);
bt_del=(Button)findViewById(R.id.btn_del);
bt_sel=(Button)findViewById(R.id.btn_sel);
bt_reg.setOnClickListener(this);
bt_del.setOnClickListener(this);
bt_sel.setOnClickListener(this);
userList=new ArrayList<User>();
}
public void onClick(View view){
switch (view.getId()) {
case R.id.btn_reg:
if (Name.getText().toString().matches("[a-zA-Z]*") && Name.getText().toString().length() > 5 && Name.getText().toString().length() < 13 && Password.getText().toString().matches("[0-9]*") && Password.getText().toString().length() > 2 && Password.getText().toString().length() < 7) {
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
db.execSQL("insert into user(name,password) values (?,?)", new String[]{Name.getText().toString(), Password.getText().toString()});
db.close();
Toast.makeText(MainActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this, "输入不合法", Toast.LENGTH_SHORT).show();
break;
case R.id.btn_del:
if (Name.getText().toString().length() != 0 && Password.getText().toString().length() != 0) {
User user = null;
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * " + "from user where name=?", new String[]{Name.getText().toString()});
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(id, name, password);
cursor.close();
if (user.getPassword().equals(Password.getText().toString())) {
db.execSQL("delete from user where name=?", new String[]{Name.getText().toString()});
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this, "密码不正确", Toast.LENGTH_SHORT).show();
db.close();
}
}
else if(Name.getText().toString().length() != 0 && Password.getText().toString().length() == 0){
User user = null;
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * " + "from user where name=?", new String[]{Name.getText().toString()});
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(id, name, password);
cursor.close();
if (user.getName().equals(Name.getText().toString())) {
db.execSQL("delete from user where name=?", new String[]{Name.getText().toString()});
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
} else
Toast.makeText(MainActivity.this, "用户不存在", Toast.LENGTH_SHORT).show();
db.close();
}
}
break;
case R.id.btn_sel:
Layout.removeAllViews();
if (Name.getText().toString().length() == 0 && Password.getText().toString().length() == 0) {
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user", null);
if (cursor.moveToFirst()) {
do {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
User user = new User(id, name, password);
userList.add(user);
} while (cursor.moveToNext());
}
for (User user : userList) {
TextView tv = new TextView(this);
tv.setText(user.toString());
tv.setTextSize(20);
Layout.addView(tv);
}
userList.clear();
cursor.close();
db.close();
} else if (Name.getText().toString().length() != 0 && Password.getText().toString().length() == 0) {
dbhelper = new MyDatabaseHelper(this, "CREATE_USER.db", null, 1);
SQLiteDatabase db = dbhelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * " + "from user where name=?", new String[]{Name.getText().toString()});
User user = null;
if (cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password = cursor.getString(cursor.getColumnIndex("password"));
user = new User(id, name, password);
cursor.close();
db.close();
TextView tv = new TextView(this);
tv.setText(user.toString());
tv.setTextSize(20);
Layout.addView(tv);
}
}
else if (Name.getText().toString().length()!=0){
dbhelper=new MyDatabaseHelper(this,"CREATE_USER.db" ,null,1);
SQLiteDatabase db=dbhelper.getReadableDatabase();
Cursor cursor=db.rawQuery("select * " + "from user where name=?",new String[]{Name.getText().toString()});
User user=null;
if(cursor.moveToFirst()){
int id = cursor.getInt(cursor.getColumnIndex("id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
String password =cursor.getString(cursor.getColumnIndex("password"));
user=new User(id,name,password);
cursor.close();
db.close();
if(user.getPassword().equals(Password.getText().toString())) {
TextView tv = new TextView(this);
tv.setText(user.toString());
tv.setTextSize(20);
Layout.addView(tv);
}
else if (Password.getText().toString().length()==0){
Toast.makeText(MainActivity.this,"用户不存在",Toast.LENGTH_SHORT).show();
}
else
Toast.makeText(MainActivity.this,"密码不正确",Toast.LENGTH_SHORT).show();
}}
break;
}}}
MyDatabaseHelper.java
public class MyDatabaseHelper extends SQLiteOpenHelper {
public static final String CREATE_USER = "create table User("
+"id integer primary key autoincrement,"
+"name text not null,"
+"password integer not null)";
private Context mContext;
public MyDatabaseHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
super(context , name ,factory,version);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_USER);
Toast.makeText(mContext, "", Toast.LENGTH_SHORT).show();
}
@Override
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("drop table if exists User");
onCreate(db);
}
}
User.java
public class User {
private String name;
private String password;
private int id;
public User(int id,String name,String password){
super();
this.id = id;
this.name = name;
this.password = password;
}
public void setId(int id){
this.id = id;
}
public void setName(String name){
this.name = name;
}
public void setPassword(String password){
this.password = password;
}
public int getId(){
return id;
}
public String getName(){
return name;
}
public String getPassword(){
return password;
}
public String toString() {
return "Id:"+this.id+" 姓名:"+this.name+" 密码:"+this.password;
}
}
SQLite的使用案例的更多相关文章
- OpenStreetMap数据清洗(SQL&MonogoDB版本)
目标:通过网上下载的OpenStreetMap.xml数据格式,将该文件的格式进行统计,清洗,并导出成CSV格式的文件,最后倒入到SQLite中 本案例中所需的包 import csv import ...
- 掘金 Android 文章精选合集
掘金 Android 文章精选合集 掘金官方 关注 2017.07.10 16:42* 字数 175276 阅读 50053评论 13喜欢 669 用两张图告诉你,为什么你的 App 会卡顿? - A ...
- 【Android自己定义View实战】之自己定义超简单SearchView搜索框
[Android自己定义View实战]之自己定义超简单SearchView搜索框 这篇文章是对之前文章的翻新,至于为什么我要又一次改动这篇文章?原因例如以下 1.有人举报我抄袭,原文链接:http:/ ...
- Android(java)学习笔记195:学生信息管理系统案例(SQLite + ListView)
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
- IOS UIPickView+sqlite 选择中国全部城市案例
1.案例简单介绍 通过读取文件.将中国全部城市写入sqlite数据库中,现通过UIPickView实现中国全部城市的选择,效果图例如以下所看到的 2.城市对象模型 中国全部城市数据请看http://b ...
- Android(java)学习笔记188:学生信息管理系统案例(SQLite + ListView)
1.首先说明一个知识点,通常我们显示布局文件xml都是如下: setContentView(R.layout.activity_main): 其实每一个xml布局文件就好像一个气球,我们可以使用Vie ...
- IOS Sqlite用户界面增删改查案例
1.案例简单介绍 对SQLite操作进行了简单的封装,将对数据表操作转变成对对象的操作,并通过UI界面完毕对用户表的增.删.改.查,执行界面例如以下图所看到的 a 2.项目project文件夹 Use ...
- SQLite数据库以及增删改查的案例
Android使用开源的与操作系统无关的SQL数据库——SQLite 一:在命令行下创建数据库: 1.启动模拟器后,打开命令行,执行adb shell 2.进入所在工程目录 3.执行sqlite3 m ...
- Android SQLite案例
重点掌握execSQL()和rawQuery()方法,rawQuery()方法用于执行select语句. SQLiteOpenHelper,实现了onCreate和onUpgrade方法. 第一次创建 ...
随机推荐
- TaskBarProgress(任务栏进度条)
原文:TaskBarProgress(任务栏进度条) </Grid> { { InitializeComponent(); Loaded += } { BackgroundWorker w ...
- 2013年新年礼物---CrossFPC 终于出来了
2012年12月份,玛雅人的预言没有实现,一个内部进行了7年开发的CrossFPC 终于见光了. 网址:http://www.crossfpc.com/ Welcome to CrossFPC, a ...
- ORA-23421: job number 225 is not a job in the job queue
在对数据库进行异机恢复之后,为了防止上面作业自动执行,扰乱正常业务系统,需要将测试库上的作业和db_link进行删除:但是使用sys用户连接进去,删除的时候报如下错误SQL> exec DBMS ...
- JS获取a标签的Href 内容
<script type="text/javascript">function getHref(obj){ alert(obj.href);} </script& ...
- Linux日志系统
常见的日志 常见的日志一般存储在/var/log中.常见的日志查看使用:ls/ll,cat/more/less查看即可:wtmp,lastlog使用last和lastlog提取其信息即可 配置日志 较 ...
- 在Delphi中编辑res文件
先用记事本编写一个rc的文件.如内容为:_Comms RCData Comms.jpg Comms.jpg为图片名称,然后在这个rc文件和图片拷贝到delphi安装路径的bin文件夹里面,选中这两个文 ...
- isHiden和isVisible的区别(可是有nativeEvent进行设置)
之前一直对isHiden和isVisible的区别比较模糊,都是乱用的.今天因需要仔细看了一下. 1.isHiden只是返回部件的隐藏属性,并不能表示部件当前的真实状态.比如A部件有个子部件B,而A处 ...
- Linux 桌面玩家指南:20. 把 Linux 系统装入 U 盘打包带走
特别说明:要在我的随笔后写评论的小伙伴们请注意了,我的博客开启了 MathJax 数学公式支持,MathJax 使用$标记数学公式的开始和结束.如果某条评论中出现了两个$,MathJax 会将两个$之 ...
- Qt 5.3更新无数,更改C++控制台输出最为赞(这样就和普通C++ IDE没区别了)
转载请注明文章:Qt 5.3更新无数,更改C++控制台输出最为赞 出处:多客博图 本人觉得有了这个更新,Qt Creator可谓几乎没有缺点了,起码仅仅开发C/C++,是不用再去安装VS了. Qt 5 ...
- Qt4学习笔记 (7) 本篇说一下Qt对于脚本的支持, 即QtScript模块.
本篇说一下Qt对于脚本的支持, 即QtScript模块. Qt支持的脚本基于ECMAScript脚本语言, 这个东西又是javascript, jscript的基础. 所以, 一般只要学过javasc ...