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 ...
随机推荐
- box-pack
box-pack表示父容器里面子容器的水平对齐方式,可选参数如下所示: start | end | center | justify <article class="wrap" ...
- js字符串使用占位符拼接
由于几个老项目中经常用到jquery拼接字符串,各种引号很disgusting 所以写了一个占位符拼接的的方法 String.prototype.signMix= function() { if(ar ...
- 洛谷P1160 队列安排
题目描述 一个学校里老师要将班上N个同学排成一列,同学被编号为1-N,他采取如下的方法: 1.先将1号同学安排进队列,这时队列中只有他一个人: 2.2-N号同学依次入列,编号为i的同学入列方式为:老师 ...
- Linux : 使用 lsof 恢复文件
用 lsof 命令在某种程度上可以恢复删除的文件, 前提是这个文件被正在运行的进程占用. 比如: 日志文件, 配置文件. lsof 恢复文件 查找需要恢复的文件和占用文件的进程 PID lsof |g ...
- OpenGL入门学习(七)(转)
http://blog.chinaunix.net/uid-20622737-id-1912803.html 今天要讲的是OpenGL光照的基本知识.虽然内容显得有点多,但条理还算比较清晰,理解起来应 ...
- OS X升级到10.10之后使用pod出现问题的解决方法
http://blog.csdn.net/dqjyong/article/details/37958067 OS X升级到10.10之后使用pod出现问题的解决方法 分类: IOS2014-07-1 ...
- spark streaming 异常No output streams registered, so nothing to execute
实现spark streaming demo时,代码: public static void main (String[] args) { SparkConf conf = new SparkConf ...
- Job的使用
1.Job完成状态监听: job.addJobChangeListener(new JobChangeAdapter() { @Override publi ...
- Xamarin.Android真机测试提示[INSTALL_FAILED_UPDATE_INCOMPATIBLE]
Xamarin.Android真机测试提示[INSTALL_FAILED_UPDATE_INCOMPATIBLE] 使用真机测试的时候,出现以下错误提示: Deployment failed ...
- centos忘记密码,重新设置密码的方法
(1)重新启动Centos,在启动过程中,长按“ESC”键,进入GNU GRUB界面. (2)选择要进入的系统,按“E”键(在启动之前编辑命令). (3)选择第二项操作系统的内核“kernel”,按& ...