查询结果图如下
代码如下:
查询结果使用的activity是dialog风格的activity,在manifest文件中如此配置
- <activity android:name=".ResultActivity"
- android:label="查询结果"
- android:theme="@android:style/Theme.Dialog"></activity>
要实现数据库的增删改查,需要三步:
1.继承SQLiteOpenHelper复写onCreate方法,一般在里面实现创建表的工作。
2.通过SQLiteOpenHelper的getReadableDatabase或者getWriteableDatabase方法创建数据库
3.调用数据库增删改查方法进行数据操作
java代码如下
- package com.example.mysqliitedatabase;
- import android.provider.BaseColumns;
- import android.provider.SyncStateContract.Columns;
- //定义数据库表的名字及表的列名
- public class Words {
- static class MyColumn implements BaseColumns{
- final static String TABLE_NAME="dict";
- final static String _ID="_id";
- final static String WORD="word";
- final static String DETAIL="detail";
- }
- }
- package com.example.mysqliitedatabase;
- import android.content.Context;
- import android.database.sqlite.SQLiteDatabase;
- import android.database.sqlite.SQLiteDatabase.CursorFactory;
- import android.database.sqlite.SQLiteOpenHelper;
- public class MySqliteDatabaseHelper extends SQLiteOpenHelper {
- String CreateTable="create table dict"+"("+Words.MyColumn._ID+"integer primary key ,"+Words.MyColumn.WORD+","+Words.MyColumn.DETAIL+")";
- public MySqliteDatabaseHelper(Context context, String name,
- CursorFactory factory, int version) {
- super(context, name, factory, version);
- // TODO Auto-generated constructor stub
- }
- @Override
- public void onCreate(SQLiteDatabase db) {
- // TODO Auto-generated method stub
- db.execSQL(CreateTable);
- }
- @Override
- public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
- // TODO Auto-generated method stub
- }
- }
- package com.example.mysqliitedatabase;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Map;
- import java.util.Stack;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.ContentValues;
- import android.content.Intent;
- import android.database.Cursor;
- import android.database.sqlite.SQLiteDatabase;
- import android.text.TextUtils;
- //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 wordString;
- private EditText detailString;
- private EditText queryString;
- private SQLiteDatabase database;
- private Button addButton;
- private Button queryButton;
- String[] returnColums={Words.MyColumn.WORD,Words.MyColumn.DETAIL};
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final MySqliteDatabaseHelper datanaseHelper=new MySqliteDatabaseHelper(getApplicationContext(), "dict.db",null,1);
- wordString=(EditText) findViewById(R.id.editText1);
- detailString=(EditText) findViewById(R.id.editText2);
- queryString=(EditText) findViewById(R.id.editText3);
- addButton= (Button) findViewById(R.id.button1);
- queryButton=(Button) findViewById(R.id.button2);
- addButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- final String word=wordString.getText().toString();
- final String detail=detailString.getText().toString();
- database=datanaseHelper.getWritableDatabase();
- ContentValues contentValues=new ContentValues();
- contentValues.put(Words.MyColumn.WORD,word);
- contentValues.put(Words.MyColumn.DETAIL,detail);
- database.insert("dict", null,contentValues);
- Toast.makeText(getApplicationContext(), "add success",1000).show();
- }
- });
- queryButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- database=datanaseHelper.getWritableDatabase();
- String query=queryString.getText().toString();
- String[] selectionArgs = null;
- String selection = null;
- Cursor cursor;
- if (!TextUtils.isEmpty(query)) {
- selectionArgs=new String[]{query};
- selection=Words.MyColumn.WORD+"= ?";
- }
- cursor=database.query("dict", returnColums, selection, selectionArgs, null,null,null);
- ArrayList<Map<String,String>> list=new ArrayList<Map<String,String>>();
- if (cursor!=null) {
- while (cursor.moveToNext()) {
- Map<String,String> map=new HashMap<String, String>();
- int wordIndex=cursor.getColumnIndex(Words.MyColumn.WORD);
- int detailIndex=cursor.getColumnIndex(Words.MyColumn.DETAIL);
- map.put(Words.MyColumn.WORD,cursor.getString(wordIndex));
- map.put(Words.MyColumn.DETAIL,cursor.getString(detailIndex));
- list.add(map);
- }
- Toast.makeText(getApplicationContext(), "query success",1000).show();
- Intent intent=new Intent(getApplicationContext(), ResultActivity.class);
- Bundle bundle=new Bundle();
- bundle.putSerializable("data",list);
- intent.putExtras(bundle);
- startActivity(intent);
- }
- else {
- Toast.makeText(getApplicationContext(), "cursor is null",1000).show();
- }
- }
- });
- }
- }
- package com.example.mysqliitedatabase;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Map;
- import android.app.Activity;
- import android.content.Intent;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class ResultActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- ListView listView=new ListView(getApplicationContext());
- setContentView(listView);
- Intent intent=getIntent();
- ArrayList<Map<String,String>> list=(ArrayList<Map<String, String>>) intent.getSerializableExtra("data");
- SimpleAdapter adapter=new SimpleAdapter(getApplicationContext(), list, R.layout.main, new String[]{Words.MyColumn.WORD,Words.MyColumn.DETAIL},new int[]{R.id.text1,R.id.text2});
- listView.setAdapter(adapter);
- }
- }