Android中怎么實現(xiàn)數(shù)據(jù)庫相關(guān)操作,針對這個問題,這篇文章詳細介紹了相對應(yīng)的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
平橋ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:028-86922220(備注:SSL證書合作)期待與您的合作!
1) Android數(shù)據(jù)庫之庫操作類:
package com.topsun;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DBHelper {
private static final String TAG = "UserDB_DBHelper.java";
private static final String DataBaseName = "UserDB";
SQLiteDatabase db;
Context context;
public DBHelper(Context context) {
this.open(context);
}
private void createTabel() {
// TODO Auto-generated method stub
String sql = "";
try {
sql = "CREATE TABLE IF NOT EXISTS TestUser (ID INTEGER
PRIMARY KEY autoincrement, NAME TEXT, SEX TEXT, AGES INTEGER)";this.db.execSQL(sql);
Log.v(TAG, "Create Table TestUser ok");
} catch (Exception e) {
Log.v(TAG, "Create Table TestUser fail");
} finally {
//this.db.close();
Log.v(TAG, "Create Table TestUser ");
}
}
public boolean save(String name, String sex, Integer ages) {
String sql = "insert into TestUser values
(null,'" + name + "','" + sex+ "'," + ages + ")";
try {
this.db.execSQL(sql);
Log.v(TAG, "insert Table TestUser 1 record ok");
return true;
} catch (Exception e) {
Log.v(TAG, "insert Table TestUser 1 record fail");
return false;
} finally {
//this.db.close();
Log.v(TAG, "insert Table TestUser ");
}
}
public Cursor loadAll() {
Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null,null, null, null, null);
return cur;
}
public void open(Context context){
if (null == db || !this.db.isOpen()){
this.context = context;
this.db = context.openOrCreateDatabase(this.DataBaseName,
context.MODE_PRIVATE, null);
createTabel();
Log.v(this.TAG, "create or Open DataBase。。。");
}
}
public void close() {
db.close();
}
}
package com.topsun;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class DBHelper {
private static final String TAG = "UserDB_DBHelper.java";
private static final String DataBaseName = "UserDB";
SQLiteDatabase db;
Context context;
public DBHelper(Context context) {
this.open(context);
}
private void createTabel() {
// TODO Auto-generated method stub
String sql = "";
try {
sql = "CREATE TABLE IF NOT EXISTS TestUser
(ID INTEGER PRIMARY KEY autoincrement,
NAME TEXT, SEX TEXT, AGES INTEGER)";this.db.execSQL(sql);
Log.v(TAG, "Create Table TestUser ok");
} catch (Exception e) {
Log.v(TAG, "Create Table TestUser fail");
} finally {
//this.db.close();
Log.v(TAG, "Create Table TestUser ");
}
}
public boolean save(String name, String sex, Integer ages) {
String sql = "insert into TestUser values
(null,'" + name + "','" + sex+ "'," + ages + ")";
try {
this.db.execSQL(sql);
Log.v(TAG, "insert Table TestUser 1 record ok");
return true;
} catch (Exception e) {
Log.v(TAG, "insert Table TestUser 1 record fail");
return false;
} finally {
//this.db.close();
Log.v(TAG, "insert Table TestUser ");
}
}
public Cursor loadAll() {
Cursor cur = db.query("TestUser", new String[]
{ "ID", "NAME","SEX","AGES"}, null,null, null, null, null);
return cur;
}
public void open(Context context){
if (null == db || !this.db.isOpen()){
this.context = context;
this.db = context.openOrCreateDatabase(this.DataBaseName,
context.MODE_PRIVATE, null);
createTabel();
Log.v(this.TAG, "create or Open DataBase。。。");
}
}
public void close() {
db.close();
}
}
2) Android數(shù)據(jù)庫交互代碼
package com.topsun;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class guiwindows extends Activity
implements OnClickListener {EditText TEditname;
EditText TEditsex;
EditText TEditages;
EditText TEditmerge;
Button TSavebutton;
Button TViewbutton;
DBHelper db;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.TEditname = (EditText) this.findViewById
(R.id.widgeteditname);this.TEditsex = (EditText) this.findViewById
(R.id.widgeteditsex);this.TEditages = (EditText) this.findViewById
(R.id.widgeteditages);TEditmerge = (EditText) this.findViewById
(R.id.widgeteditmerge);this.TSavebutton = (Button) this.findViewById
(R.id.widgetSavebutton);TViewbutton = (Button) this.findViewById
(R.id.widgetViewbutton);this.db = new DBHelper(this);
this.TSavebutton.setOnClickListener(this);
this.TViewbutton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// this.TEditages.setText(this.TEditname.getText().
toString()+this.TEditsex.getText().toString());if (v.getId() == R.id.widgetSavebutton) {
try {
this.db.open(this);
this.db.save(this.TEditname.getText().toString(), this.TEditsex
.getText().toString(), Integer.valueOf(this.TEditages
.getText().toString()));
} catch (Exception e) {
Log.v("save data", "save data fail");
} finally {
this.db.close();
}
} else if (v.getId() == R.id.widgetViewbutton && null != db) {
this.db.open(this);
// 瀏覽所有數(shù)據(jù)
Cursor cur = db.loadAll();
StringBuffer sf = new StringBuffer();
cur.moveToFirst();
while (!cur.isAfterLast()) {
sf.append(cur.getInt(0)).append(" : ").append(cur.getString(1))
.append(" : ").append(cur.getString(2)).append(" : ")
.append(cur.getInt(3)).append("\n");
cur.moveToNext();
}
db.close();
this.TEditmerge.setText(sf.toString());
}
}
}
關(guān)于Android中怎么實現(xiàn)數(shù)據(jù)庫相關(guān)操作問題的解答就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道了解更多相關(guān)知識。
新聞名稱:Android中怎么實現(xiàn)數(shù)據(jù)庫相關(guān)操作
轉(zhuǎn)載源于:http://bm7419.com/article0/jjccio.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供云服務(wù)器、用戶體驗、微信公眾號、做網(wǎng)站、網(wǎng)站策劃、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)