本文目錄一覽:
sqlite資料庫從安裝到使用的方法,因為我看了之後不知道要不要安裝了
sqlite資料庫是不需要安裝的。
使用sqlite程序可以生成一個資料庫文件。例如test.db (文件名自己設定,擴展名也無要求)
當然,你的程序必須要支持sqlite,例如php是在配置中設置。
之後就可以在程序中調用sqlite的函數來操作這個文件。
如何使用sqlite 資料庫文件
在我幾個Android應用中,我需要訪問已有的資料庫。這些資料庫往往很大,甚至超過asset文件大約1兆位元組的限制。而且在新的版本中資料庫需要更新。我在網上,特別是StackOverflow看了一些文章,並做了一些試驗,覺得下面的代碼能基本上滿足我的需求。
其主要思路是:
1. 把資料庫分解成幾個asset文件。
2. 當需要打開資料庫時,如果資料庫不存在,就把那幾個asset文件重新合併成一個資料庫文件。
3. 如果資料庫的版本改變了,就在onUpgrade()方法中把資料庫文件刪除掉。
下面是代碼:
//資料庫的預設路徑
private static finalString DB_PATH = “/data/data/com.mypackage.myapp/databases/”;
private static finalString DB_NAME = “mydb.db”;
private static finalint DB_VERSION = 2;
private static finalString DB_SPLIT_NAME = “mydb.db.00”;
private static finalint DB_SPLIT_COUNT = 3;
private SQLiteDatabasem_database;
private final Contextm_context;
/**
* Constructor
*保存傳進來的context參數以用來訪問應用的asset和資源文件。
* @param context
*/
public MyDB(Contextcontext) {
super(context, DB_NAME, null, DB_VERSION);
this.m_context = context;
}
public static MyDBopenDatabaseReadOnly(Context context) {
MyDB db = new MyDB(context);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDataBase(SQLiteDatabase.OPEN_READONLY);
return db;
}
public static MyDBopenDatabaseReadWrite(Context context) {
MyDB db = new MyDB(context);
try {
db.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
db.openDataBase(SQLiteDatabase.OPEN_READWRITE);
return db;
}
/**
*創建一個空資料庫,用來存儲你已有的資料庫。
*/
public voidcreateDataBase() throws IOException{
boolean dbExist =checkDataBase();
if (dbExist) {
/*
**如果你的資料庫的版本改變了,調用這個方法確保在onUpgrade()被調用時
**傳進去的是可寫的資料庫。
*/
SQLiteDatabase db =this.getWritableDatabase();
if (db != null) {
db.close();
}
}
dbExist = checkDataBase();
if (!dbExist) {
你好 我們也是要用MY SQL實現資料庫的增刪改查
一、—資料庫創建—
想要對資料庫進行增刪改查,首先要創建一個資料庫,安卓中的資料庫是使用sqlite,我們是通過調用SQLiteOpenHelper類來進行資料庫的創建的和操作的。
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.iigt.crud;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;public class MySqlite extends SQLiteOpenHelper {public MySqlite(Context context) {
//context :上下文 , name:資料庫文件的名稱 factory:用來創建cursor對象,默認為null//version:資料庫的版本號,從1開始,如果發生改變,onUpgrade方法將會調用,4.0之後只能升不能將super(context, “info.db”, null,1);
}
//oncreate方法是資料庫第一次創建的時候會被調用; 特別適合做表結構的初始化,需要執行sql語句;SQLiteDatabase db可以用來執行sql語句@Override
public void onCreate(SQLiteDatabase db) {//通過SQLiteDatabase執行一個創建表的sql語句db.execSQL(“create table info (_id integer primary key autoincrement,name varchar(20),phone varchar(11))”);}
//onUpgrade資料庫版本號發生改變時才會執行; 特別適合做表結構的修改@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {//添加一個phone欄位
//db.execSQL(“alter table info add phone varchar(11)”);}
}
二、—操作類—
定義完了資料庫,再就是定義我們使用的往資料庫中存數據的函數了,也就是我們通常所指的DAO函數,不過在定義DAO函數之前,先要定義一個bean類,要不想要操作的數據無從而來。如下是bean和DAO。
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.iigt.bean;
public class InfoBean {
public String name;
public String phone;
}
DAO的代碼如下:
[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.iigt.dao;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;import com.iigt.bean.InfoBean;
import com.iigt.crud.MySqlite;
public class InfoDao {
private MySqlite mySqliteOpenHelper;
public InfoDao(Context context){
//創建一個幫助類對象
mySqliteOpenHelper = new MySqlite(context);}
public void add(InfoBean bean){
//調用getReadableDatabase方法,來初始化資料庫的創建SQLiteDatabase db = mySqliteOpenHelper.getWritableDatabase();//sql:sql語句, bindArgs:sql語句中佔位符的值db.execSQL(“insert into info(name,phone) values(?,?);”, new Object[]{bean.name,bean.phone});//關閉資料庫對象
db.close();
}
public void del(String name){
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();db.execSQL(“delete from info where name=?;”, new Object[]{name});db.close();
}
public void update(InfoBean bean){
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();db.execSQL(“update info set phone=? where name=?;”, new Object[]{bean.phone,bean.name});db.close();
}
public void query(String name){
//執行sql語句需要sqliteDatabase對象
//調用getReadableDatabase方法,來初始化資料庫的創建SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();//sql:sql語句, selectionArgs:查詢條件佔位符的值,返回一個cursor對象Cursor cursor = db.rawQuery(“select _id, name,phone from info where name = ?;”, new String []{name});//解析Cursor中的數據
if(cursor != null cursor.getCount() 0){//判斷cursor中是否存在數據//循環遍歷結果集,獲取每一行的內容
while(cursor.moveToNext()){//條件,游標能否定位到下一行//獲取數據
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println(“_id:”+id+”;name:”+name_str+”;phone:”+phone);}
cursor.close();//關閉結果集
}
//關閉資料庫對象
db.close();
}
}
三、—操作界面—
定義一操作界面來對資料庫進行操作,操作界面包括增刪改查。
[html] view plain copy print?在CODE上查看代碼片派生到我的代碼片LinearLayout xmlns:android=””xmlns:tools=””android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”android:paddingLeft=”@dimen/activity_horizontal_margin”android:paddingRight=”@dimen/activity_horizontal_margin”android:paddingTop=”@dimen/activity_vertical_margin”android:orientation=”vertical”
tools:context=”.MainActivity”
Button
android:id=”@+id/bt_add”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/add” /
Button
android:id=”@+id/bt_del”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/del” /
Button
android:id=”@+id/bt_update”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/update” /
Button
android:id=”@+id/bt_query”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/query” /
/LinearLayout
四、—主函數—
(1)創建一個layout界面,其中包含「增刪改查」4個控制項。
(2)獲取這四個控制項,並根據各個控制項的ID來判斷點擊的是哪個事件(3)此時如果是判斷是點擊了add事件,就應該增加了,所以需要調用增加函數(4)需要先創建一個javabean和它的操作類DAO(5)dao是要向資料庫中增加數據,要創建一個資料庫並進行初始化(6)在dao類中定義向資料庫中的增刪改查方法[java] view plain copy print?在CODE上查看代碼片派生到我的代碼片package com.iigt.crud;
import android.app.Activity;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;import com.iigt.bean.InfoBean;
import com.iigt.dao.InfoDao;
import com.iigt.adddeldatabase.*;
public class MainActivity extends Activity implements OnClickListener {private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = this;
//創建一個幫助類對象
MySqlite mySqliteOpenHelper = new MySqlite(mContext);//調用getReadableDatabase方法,來初始化資料庫的創建SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();//找到相應控制項
findViewById(R.id.bt_add).setOnClickListener(this);findViewById(R.id.bt_del).setOnClickListener(this);findViewById(R.id.bt_update).setOnClickListener(this);findViewById(R.id.bt_query).setOnClickListener(this);}
@Override
public void onClick(View v) {
InfoDao infoDao = new InfoDao(mContext);//創建一個dao對象做增刪改查switch (v.getId()) {
case R.id.bt_add:
InfoBean bean = new InfoBean();
bean.name = “張三”;
bean.phone =”110″;
infoDao.add(bean);
InfoBean bean1 = new InfoBean();
bean1.name = “李四”;
bean1.phone =”120″;
infoDao.add(bean1);
break;
case R.id.bt_del:
infoDao.del(“張三”);
infoDao.del(“李四”);
break;
case R.id.bt_update:
InfoBean bean2 = new InfoBean();
bean2.name = “張三”;
bean2.phone =”119″;
infoDao.update(bean2);
break;
case R.id.bt_query:
infoDao.query(“張三”);
infoDao.query(“李四”);
break;
default:
break;
}
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/189879.html