基于HTTP登录系统的扩展(SQLIte数据库部分)

下午在家写android程序。把数据库部分通过写代码方式实践了一下:

注意:产生的数据库文件New.db在windows下边查找不到,需要借助DDMS.找到data/data/com.example/databases,然后进行导数据

核心代码如下:

package com.example;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Color;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
/**
 * 针对SQLite数据库进行操作
 * @author 码农小江
 *
 */
public class Five extends Activity{
	private static int miscount = 1;
	/*数据库对象*/
	private SQLiteDatabase mSqLiteDatabase = null;
	/*数据库名*/
	private  final static String DATABASE_NAME = "New.db";
	/*表名*/
	private  final static String TABLE_NAME = "PHP";
	/*表中字段*/
	private  final static String TABLE_ID = "_id";
	private  final static String TABLE_NUM = "num";
	private  final static String TABLE_DATA = "data";
	public   ListView  m_ListView;
	public  LinearLayout m_LinearLayout;
	
	/*创建表的SQL语句*/
	private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME +
	                               "(" + TABLE_ID + " INTEGER PRIMARY KEY, "
	                                   +  TABLE_NUM + " INTERGER, "
	                                   +  TABLE_DATA + " TEXT)";  
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		       //创建LinearLayout线性布局
				m_LinearLayout = new LinearLayout(this);
				//设置LinearLayout布局的属性
				m_LinearLayout.setOrientation(LinearLayout.VERTICAL);
				m_LinearLayout.setBackgroundColor(android.graphics.Color.BLACK);
				//创建ListView对象
				m_ListView = new ListView(this);
				LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
						                                           LinearLayout.LayoutParams.WRAP_CONTENT);
				m_ListView.setBackgroundColor(Color.BLACK);
				//添加m_listView到m_LinerLayout中
				m_LinearLayout.addView(m_ListView,param);
				//设置主显页面
				setContentView(m_LinearLayout);
		
		//打开已经存在的数据库
		mSqLiteDatabase = this.openOrCreateDatabase(DATABASE_NAME, MODE_PRIVATE, null);
		//获取数据库的Cursor
		try{
			//在数据库中创建一个表
			mSqLiteDatabase.execSQL(CREATE_TABLE);
			
		}catch(Exception e){
			UpdataAdapter();
		}
	}
	public boolean onKeyUp(int keyCode, KeyEvent event){
		switch(keyCode){
		case KeyEvent.KEYCODE_DPAD_LEFT:
			AddData();
			break;
		
		case KeyEvent.KEYCODE_DPAD_RIGHT:
			DeleteData();
			break;
		
		case KeyEvent.KEYCODE_1:
			UpData();
			break;
		
		case KeyEvent.KEYCODE_2:
			DeleteTable();
			break;
		
		case KeyEvent.KEYCODE_3:
			DeleteDatabase();
			break;
		
		}
		return true;
	}
	
	/**
	 * 删除数据库
	 */
	public void DeleteDatabase(){
		this.deleteDatabase(DATABASE_NAME);
		this.finish();
	}
	
	/**
	 * 删除表操作
	 */
	public void DeleteTable(){
		mSqLiteDatabase.execSQL("DROP TABLE " + TABLE_NAME);
		this.finish();
	}
	
	/**
	 * 更新一条数据操作
	 */
	public void UpData(){
		ContentValues cv = new ContentValues();
		cv.put(TABLE_NUM, miscount);
		cv.put(TABLE_DATA, " 修改后的数据 " + miscount);
		//更新数据库
		mSqLiteDatabase.update(TABLE_NAME, cv, TABLE_NUM + " = " + Integer.toString(miscount-1), null);
		UpdataAdapter();
	}
	/**
	 * 向表中添加一条数据
	 */
	public void AddData(){
		ContentValues cv = new ContentValues();
		cv.put(TABLE_NUM, miscount);
		cv.put(TABLE_DATA, " 测试数据库数据 " + miscount);
		/*插入数据*/
		mSqLiteDatabase.insert(TABLE_NAME, null, cv);
		miscount++;
		UpdataAdapter();
	}
	
	/**
	 * 从表中删除一个指定的数据
	 */
	public void DeleteData(){
		//删除数据
		mSqLiteDatabase.execSQL(" DELETE FROM " + TABLE_NAME + " WHERE _id = " + Integer.toString(miscount));
		miscount--;
		if(miscount < 0){
			miscount = 0;
		}
		UpdataAdapter();
	}
	
	/**
	 * 更新视图显示
	 */
	public void UpdataAdapter(){
		//获取数据库的cursor
		Cursor cur = mSqLiteDatabase.query(TABLE_NAME, new String[]{TABLE_ID,TABLE_NUM,TABLE_DATA},
				                 null, null, null, null, null);
		miscount = cur.getCount();
		if(cur != null && cur.getCount() >= 0){
			ListAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, 
					cur, new String[]{TABLE_NUM,TABLE_DATA}, new int[]{android.R.id.text1,android.R.id.text2});
			m_ListView.setAdapter(adapter);
		}
	}
	/**
	 * 按键处理
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event ){
		if(keyCode == KeyEvent.KEYCODE_BACK){
			mSqLiteDatabase.close();
			this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
}

 

posted @ 2012-05-13 17:03  码农小江  阅读(359)  评论(0编辑  收藏  举报