android数据库 Android自带数据库SQLite 操作 一步一步 图示教程

开发过程中使用Android自带的数据库,不仅安全性较好,而且数据存取的效果也较好。

附源代码

http://download.csdn.net/detail/wyx100/8670843

开发规划

主界面

 完成数据的输入,触发保存 

启动后界面

android数据库 Android自带数据库SQLite 操作 一步一步 图示教程_第1张图片

 

输入数据

android数据库 Android自带数据库SQLite 操作 一步一步 图示教程_第2张图片

 数据显示界面

     数据库读取数据后显示

android数据库 Android自带数据库SQLite 操作 一步一步 图示教程_第3张图片

删除记录

 android数据库 Android自带数据库SQLite 操作 一步一步 图示教程_第4张图片

开发顺序

主界面

   类代码

package com.example.sqlitetest;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class ActivityMain_1 extends Activity {
	//声明EditText实例
	private EditText et1,et2,et3;
	//声明Button实例
	private Button button;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_1);
		
		//根据ID获得实例对象
		et1 = (EditText) findViewById(R.id.EditText01);
		et2 = (EditText) findViewById(R.id.EditText02);
		et3 = (EditText) findViewById(R.id.EditText03);
		button = (Button) findViewById(R.id.button);
		
		//事件
		button.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				String name=et1.getText().toString();
				String url =et2.getText().toString();
				String desc = et3.getText().toString();
			
				ContentValues values = new ContentValues();
				
				values.put("name", name);
				values.put("url", url);
				values.put("desc", desc);
				
				//实例化数据库帮助类
				DBHelper helper = new DBHelper(getApplicationContext());
				//打开数据库
				helper.open(); 
				
				//插入数据
				helper.insert(values);
				//实例化Intent
				Intent intent = new Intent(ActivityMain_1.this,QueryActivity.class);
				startActivity(intent);
				helper.close();
			}
		});
		
	}
	
	
}

   界面布局



	
	
	
	
	
	
	
	
	

2.数据显示界面

类代码

package com.example.sqlitetest;

import android.app.AlertDialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class QueryActivity extends ListActivity {
	
	private ListView listView;
	private Cursor c;
	 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		//setContentView(R.layout.row);
		
		//实例化数据库帮助类
		final DBHelper help = new DBHelper(this);
		help.open();
		c= help.query();
		 query();
		//提示对话框
		final AlertDialog.Builder builder = new AlertDialog.Builder(this);
		listView.setOnItemClickListener(new OnItemClickListener(){
			@Override
			public void onItemClick(AdapterView parent, View view,int position, long id) {
				final long temp = id;
				builder.setMessage("真的要删除该条记录吗?").setPositiveButton("是", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						System.out.println("yes");
						help.del((int)temp);
						//重新查询
						//query();
						Cursor c = help.query();
						//列表项数组
						String[] from ={"_id","name","url","desc"};
						//列表项ID
						int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3};
						//适配器
						SimpleCursorAdapter adapter = new SimpleCursorAdapter(getApplicationContext(),R.layout.row,c,from,to);
						ListView listView = getListView();
						listView.setAdapter(adapter);
						
						
					}
				}).setNegativeButton("否", new DialogInterface.OnClickListener() {
					
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						
					}
				});
				builder.create();
				builder.show();
			}
		});
		
	}
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		
	}
	public void query(){
		//列表项数组
		String[] from ={"_id","name","url","desc"};
		//列表项ID
		int[] to ={R.id.text0,R.id.text1,R.id.text2,R.id.text3};
		
		//适配器
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.row,c,from,to);
		//adapter.notifyDataSetChanged();
		//列表视图
		 listView = getListView();
		//为列表视图添加适配器
		listView.setAdapter(adapter);
		
	}
	
	
}

界面布局



	
	
	
	
	
	

数据库处理类

package com.example.sqlitetest;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DBHelper extends SQLiteOpenHelper {
	
	private static final String DB_NAME="coll.db";
	//定义表名
	private static final String TBL_NAME="logtable";
	//建立数据库表SQL语句
	private static final String CREATE_TBL = "create table logtable " +
			"(_id integer primary key autoincrement,name text,url text,desc text)";
	//SQLiteDatabase
	private SQLiteDatabase db;
	
	public DBHelper(Context context) {
		super(context, DB_NAME, null, 2);
		
	}
	@Override
	public void onCreate(SQLiteDatabase db) {
		
		db.execSQL(CREATE_TBL);
	}
	
	//打开数据库
	public void open(){
		db = getWritableDatabase();
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		// TODO Auto-generated method stub
	}
	
	//添加记录
	public void insert(ContentValues values){
		
		db.insert(TBL_NAME, null, values);
	}
	
	//记录查询
	public Cursor query(){
		Cursor c = db.query(TBL_NAME, null, null, null, null, null, null);
		return c;
	}
	
	//删除记录
	public void del(int id){
		db.delete(TBL_NAME, "_id=?", new String[]{String.valueOf(id)});
	}
	
	//关闭数据库
	public void close(){
		if (db != null){
			db.close();
		}
	}
}

启动配置文件

AndroidManifest.xml




    

    
        
            
                

                
            
        
        
    



 


 

你可能感兴趣的:(android,教程,android,数据库,自带数据库,SQLite操作)