android SQLite(单词的添加与查询应用)
本人小白,刚接触android,为方便记忆,将平时练习的代码写下来,跟大家分享,也希望大神批评指正。
这个实例主要用到的SQLite数据库的操作,可以向数据库添加单词,查询,修改以及删除单词,描述如有不当之处,还请帮忙纠正,下面上源码。
------------------------------------------我是邪恶的分割线----------------------------------------------
下面是java文件
1.创建数据库:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper; public class DatabaseHelper extends SQLiteOpenHelper { //定义创建数据表的语句
private static final String CREATE_TABLES="create table word_dic(_id INTEGERT PRIMARY KEY AUTOINCREMENT,word,detail)"; //定义构造函数
public DatabaseHelper(Context context,String name,int version){
super(context,name,null,version);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLES);
} @Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
// TODO Auto-generated method stub } }
2.创建界面UI
import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity {
private EditText word;
private EditText detail;
private Button insert;
private Button query;
private Button update;
private Button delete;
DatabaseHelper dbHelper=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //获取控件对象
word=(EditText)findViewById(R.id.word);
detail=(EditText)findViewById(R.id.detail);
insert=(Button)findViewById(R.id.insert);
query=(Button)findViewById(R.id.query);
update=(Button)findViewById(R.id.update);
delete=(Button)findViewById(R.id.delete); //生成DatabaseHelper对象
dbHelper=new DatabaseHelper(MainActivity.this,"english_db",1); //两个EditText用来获取用户的输入
//String wordStr=word.getText().toString();
//String detailStr=detail.getText().toString(); //**************************
//为按钮绑定相应的监听器
//************************** //添加单词
insert.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
String wordStr=word.getText().toString();
String detailStr=detail.getText().toString();
ContentValues values=new ContentValues();
values.put("word", wordStr);
values.put("detail", detailStr);
db.insert("word_dic", null, values);
Toast.makeText(MainActivity.this, "添加单词成功", Toast.LENGTH_LONG).show();
}
}); //查询单词
query.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getReadableDatabase();
String wordStr=word.getText().toString(); Cursor cursor=db.query("word_dic", new String[]{"_id","word","detail"}, "word like ?",
new String[]{wordStr},null, null, null);
ArrayList<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
while(cursor.moveToNext()){
HashMap<String, String> map=new HashMap<String, String>();
map.put("word", cursor.getString(cursor.getColumnIndex("word")));
map.put("detail", cursor.getString(cursor.getColumnIndex("detail")));
list.add(map);
}
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
Bundle bundle=new Bundle();
bundle.putSerializable("data", list);
intent.putExtras(bundle);
startActivity(intent);
}
}); //修改单词
update.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
String wordStr=word.getText().toString();
String detailStr=detail.getText().toString();
ContentValues values=new ContentValues();
values.put("word", wordStr);
values.put("detail", detailStr);
db.update("word_dic", values, "word like ?", new String[]{wordStr});
Toast.makeText(MainActivity.this, "单词已更新", Toast.LENGTH_LONG).show();
}
}); //删除单词
delete.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db=dbHelper.getWritableDatabase();
String wordStr=word.getText().toString(); db.delete("word_dic", "word like ?", new String[]{wordStr});
Toast.makeText(MainActivity.this, "单词已删除", Toast.LENGTH_LONG).show();
}
});
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
} }
3.创建查询结果的ACTIVITY:
import java.util.ArrayList;
import java.util.HashMap; import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class OtherActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result); listView=(ListView)findViewById(R.id.listView);
Intent intent=getIntent();
Bundle bundle=intent.getExtras(); @SuppressWarnings("unchecked")
ArrayList<HashMap<String, String>> list=(ArrayList<HashMap<String,String>>)bundle.getSerializable("data");
SimpleAdapter adapter=new SimpleAdapter(this, list, R.layout.option, new String[]{"word","detail"},
new int[]{R.id.wordShow,R.id.detailShow});
listView.setAdapter(adapter);
} }
*********************************以下是用到的资源文件*********************************************
1.main.xml:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow android:paddingLeft="10dp">
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="单词:"
android:textSize="16sp"
/>
<EditText
android:id="@+id/word"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:hint="请输入单词"
/>
</TableRow>
<TableRow android:paddingLeft="10dp">
<TextView
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="释意:"
android:textSize="16sp"
/>
<EditText
android:id="@+id/detail"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:selectAllOnFocus="true"
android:hint="请输入单词的解释"
/>
</TableRow>
<Button
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加单词"
/>
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="查询单词"
/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="修改单词"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除单词"
/>
</TableLayout>
2.result.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ListView
android:id="@+id/listView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView> </LinearLayout>
3.option.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" > <TextView
android:id="@+id/wordShow"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/> <TextView
android:id="@+id/detailShow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/wordShow"
/> </RelativeLayout>
android SQLite(单词的添加与查询应用)的更多相关文章
- Android SQLite 通配符查询找不到参数问题
使用Android SQLite中SQLiteDatabase类的query方法查询时,如果where中包含通配符,则参数会无法设置,如类似下面的方法查询时 SQLiteDatabase db = d ...
- Android Sqlite 数据库版本更新
Android Sqlite 数据库版本更新 http://87426628.blog.163.com/blog/static/6069361820131069485844/ 1.自己写一个类继承 ...
- Android SQLite总结(一) (转)
Android SQLite总结(一) 郑海波 2012-08-21 转载请声明:http://blog.csdn.net/nuptboyzhb/article/details/7891887 前言 ...
- Android SQLite 数据库详细介绍
Android SQLite 数据库详细介绍 我们在编写数据库应用软件时,需要考虑这样的问题:因为我们开发的软件可能会安装在很多用户的手机上,如果应用使用到了SQLite数据库,我们必须在用户初次使用 ...
- Android SQLite简介
SQLite 一个非常流行的嵌入式数据库,它支持 SQL 语言,并且只利用很少的内存就有很好的性能.此外它还是开源的,任何人都可以使用它.许多开源项目((Mozilla, PHP, Python)都使 ...
- Android SQLite 数据库 增删改查操作
Android SQLite 数据库 增删改查操作 转载▼ 一.使用嵌入式关系型SQLite数据库存储数据 在Android平台上,集成了一个嵌入式关系型数据库--SQLite,SQLite3支持NU ...
- Android SQLite总结[转载]
[转载] :http://blog.163.com/zqy216_2008/blog/static/4119371820119954812509/ 最近在做的项目涉及到了SQLite,大学时没有好好学 ...
- Android SQLite与ListView的简单使用
2017-04-25 初写博客有很多地方都有不足,希望各位大神给点建议. 回归主题,这次简单的给大家介绍一下Android SQLite与ListView的简单使用sqlite在上节中有介绍,所以在这 ...
- xamarin.Android SQLite存储
在可移植类库 新建: using SQLite.Net.Interop; using System; using System.Collections.Generic; using System.Li ...
随机推荐
- Registering RHEL6 Clients into spacewalk
Before Starting(login to spacwalk server) 1.Create a base channel within Spacewalk (Channels > Ma ...
- touchSlider插件案例
<!doctype html> <html lang="en"> <head> <title>jQuery手机图片触屏滑动轮播效果代 ...
- [ CodeVS冲杯之路 ] P3115
不充钱,你怎么AC? 题目:http://codevs.cn/problem/3115/ 基础的高精度减法,先判断一下被减数是否小于减数,若是则交换位置,打上 “-” 负号 当然也可以用压位做 #in ...
- 解决_CRT_SECURE_NO_WARNINGS 警告
问题:我们在程序中使用fopen等CRT函数,就会出现一些警告信息,很烦人,如下: 1>e:/project/htt/ishow/functions.cpp(156) : warning C49 ...
- OpenGL函数思考-glColor
http://blog.csdn.net/shuaihj/article/details/7231980 OpenGL函数思考-glColor 函数原型: glColor3b,glColor ...
- android hook 框架 libinject 如何实现so注入
前面两篇 android hook 框架 libinject2 简介.编译.运行 android hook 框架 libinject2 如何实现so注入 实际运行并分析了 Android中的so注入( ...
- (14)oracle数据字典
http://czmmiao.iteye.com/blog/1258462 数据字典解释 1.user_tables 查询用户所拥有的所有表 select table_name from user_t ...
- 2017 ACM-ICPC Asia Xi'an Problem A XOR(异或线性基 )
题目链接 2017西安赛区 Problem A 题意 给定一个数列,和$q$个询问,每个询问中我们可以在区间$[L, R]$中选出一些数. 假设我们选出来的这个数列为$A[i_{1}]$, $A[ ...
- C语言基础之注释与常见错误
总结起来,注释有三种: 1.单行注释 1: //哈哈 单行注释 2.多行注释 1: /* 2: asdfasdfasdfasdfasdf 3: */ 其中多行注释如果这样写 1: /* 2: * 函数 ...
- 一致性hash-java实现treemap版
把不同号段的数据储存在不同的机器上,以用来分散压力.假如我们有一百万个QQ号,十台机器,,如何划分呢? 最简单粗暴的方法是用QQ号直接对10求余,结果为0-9 分别对应上面的十台机器.比如QQ号为 2 ...