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

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

附源代码

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

开发规划

 主界面

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

 

启动后界面

技术分享

 

输入数据

技术分享

 数据显示界面

     数据库读取数据后显示

技术分享

删除记录

 技术分享

开发顺序

主界面

   类代码

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();
			}
		});
		
	}
	
	
}

 

   界面布局

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="vertical"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
	
	<TextView
		android:id="@+id/TextView01"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="网站名称"
	/>
	<EditText
		android:id="@+id/EditText01"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		
	/>
	<TextView
		android:id="@+id/TextView02"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="URL"
	/>
	<EditText
		android:id="@+id/EditText02"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
	/>
	<TextView
		android:id="@+id/TextView03"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:text="网站描述"
	/>
	<EditText
		android:id="@+id/EditText03"
		android:layout_width="fill_parent"
		android:layout_height="wrap_content"
		android:height="100px"
	/>
	
	<Button
		android:id="@+id/button"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:text="添加"
	/>
</LinearLayout>

 

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);
		
	}
	
	
}


界面布局

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	android:layout_gravity="center_vertical"
	>
	
	<TextView
		android:id="@+id/text0"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingRight="10px"
	/>
	<TextView
		android:id="@+id/text1"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingRight="10px"
	/>
	<TextView
		android:id="@+id/text2"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingRight="10px"
	/>
	<TextView
		android:id="@+id/text3"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:paddingRight="10px"
	/>
	
</LinearLayout>

数据库处理类

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.sqlitetest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"  
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.sqlitetest.ActivityMain_1"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name="com.example.sqlitetest.QueryActivity"></activity>
    </application>

</manifest>


 


 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。