android数据库、安卓数据库读写

这是一个简单的安卓数据库的创建、删除、插入和查询

话再次不多说,上代码:

package CGDBUtils;

import java.util.ArrayList;
import java.util.List;

import Data.BaseData;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
 * Author: wangliu_petter 叉哥
 * QQ:1206420658
 * Date: 16-3-24
 * Time: 18:00
 */
public class DBHelper extends SQLiteOpenHelper{
	private final static int DB_VERSON = 7;//数据库版本信息
	private Context mcContext = null;
	
	//��Table1
	private final static String TABLE_1="table1";
	private final static String Table1_appId="appId";
	private final static String Table1_appName="appName";
	private final static String Table1_pkgName="pkgName";
	private final static String Table1_country="country";
	private final static String Table1_language="language";

	public DBHelper(Context context) {
		super(context, null, null, DB_VERSON);
		// TODO Auto-generated constructor stub
		this.mcContext = context;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		
		createTable(db);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		
		dropTable(db);
	}

	
	public void createTable(SQLiteDatabase db) {

	}


	public void dropTable(SQLiteDatabase db) {

	}
	//创建数据库
	private void createTable1(SQLiteDatabase db){
		String sql="create table if not exists " + TABLE_1 + "(" + Table1_appId
				+ " integer primary key autoincrement, " 
				+ Table1_appName + " text, "
				+ Table1_pkgName + " text, "
				+ Table1_country + " text, "
                + Table1_language + " text); "; 
	}
	//删除数据库
	private void dropTable1(SQLiteDatabase db){
		try {
			db.execSQL("drop table if exists  " + TABLE_1);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	//插入数据库
	public boolean insertTable1(BaseData data){
		if(data==null){
			return false;
		}
		SQLiteDatabase db = null;
		ContentValues values = new ContentValues();
		values.put(Table1_appId, data.appId);
		values.put(Table1_appName, data.appName);
		values.put(Table1_pkgName, data.pkgName);
		values.put(Table1_country, data.country);
		values.put(Table1_language, data.language);
		long row = -1;
		try {
			db = getWritableDatabase();
			row = db.insert(TABLE_1, null, values);
			db.close();
			db = null;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (db != null) {
				db.close();
			}
		}
		return (row == -1) ? false : true;
	}
	//从数据库中取数据
	public List getBaseDatalist() {
		List list=new ArrayList();
		String sql = "select * from " + TABLE_1 ; //+" where "+Col_showtype +" = 2";
		Cursor cursor = null;
		SQLiteDatabase db = null;
         try {
        	 db = getReadableDatabase();
 			cursor = db.rawQuery(sql, null);
 			if (cursor != null) {
 				while (cursor.moveToNext()) {
					BaseData data = new BaseData();
					data.appId = cursor.getInt(cursor.getColumnIndex(Table1_appId));
					data.appName=cursor.getString(cursor.getColumnIndex(Table1_appName));
					data.pkgName=cursor.getString(cursor.getColumnIndex(Table1_pkgName));
					data.country=cursor.getString(cursor.getColumnIndex(Table1_country));
					data.language=cursor.getString(cursor.getColumnIndex(Table1_language));
					list.add(data);
				//	InsertManager.getInstance().adInsertDatalist.add(data);  
 				}
 			}
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}finally{
			if (cursor != null) {
				cursor.close();
			}
			if (db != null) {
				db.close();
			}
		}
			return list;
	}
	
	

}

你可能感兴趣的:(android)