你通過getText()方法首先得到輸入的值,然后調(diào)用數(shù)據(jù)庫(kù)的插入方法 db.insert();插入到數(shù)據(jù)庫(kù)中就行 就想這樣 \x0d\x0aEditText et ; \x0d\x0aString num = et.getText().toString(); \x0d\x0apublic void addData(String num) { \x0d\x0a SQLiteDatabase db = dbHelper.getWritableDatabase(); \x0d\x0a ContentValues values = new ContentValues(); \x0d\x0a values.put("num", num); \x0d\x0a db.insert("表名", null, values); \x0d\x0a } \x0d\x0a \x0d\x0a當(dāng)你調(diào)用這個(gè) addData()方法時(shí)就會(huì)向數(shù)據(jù)庫(kù)中插入數(shù)據(jù)了
陜州ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場(chǎng)景,ssl證書未來市場(chǎng)廣闊!成為成都創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場(chǎng)價(jià)格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:18980820575(備注:SSL證書合作)期待與您的合作!
一、引入
數(shù)據(jù)庫(kù)創(chuàng)建的問題解決了,接下來就該使用數(shù)據(jù)庫(kù)實(shí)現(xiàn)應(yīng)用程序功能的時(shí)候了?;?/p>
本的操作包括創(chuàng)建、讀取、更新、刪除,即我們通常說的 CRUD(Create, Read, Update, Delete)。
在實(shí)現(xiàn)這些操作的時(shí)候,我們會(huì)使用到兩個(gè)比較重要的類 SQLiteDatabase 類和 Cursor 類。
二、創(chuàng)建表
1,execSQL(String sql):執(zhí)行一條 sql 語句,且執(zhí)行操作不能為 SELECT
因?yàn)樗姆祷刂禐?void,所以推薦使用 insert、update 方法等
2.,execSQL (String sql,Object[] bindArgs)
sql:執(zhí)行一條 sql 語句
bindArgs:為 sql 語句中的?賦值
三、添加數(shù)據(jù)
1、execSQL(String sql)
2、使用對(duì)象的 insert 方法
ContentValues values = new ContentValues();
values.put(USERNAME, user.getUsername());
values.put(PASSWORD, user.getPassword());
db.insert(TABLE_NAME, null, values);
參數(shù):
table:數(shù)據(jù)庫(kù)中的表名
nullColumnHack:指定默認(rèn)插入字段,為 null 時(shí)能插入數(shù)據(jù)
values:表示插入字段所對(duì)應(yīng)的值,使用 put 方法。
四、刪除數(shù)據(jù)
1、execSQL(String sql)
2、使用對(duì)象的 delete 方法
String whereClaues="_id=?";
String [] whereArgs={String.valueOf(id)};
//db.delete(TABLE_NAME, "_id="+id, null);
db.delete(TABLE_NAME, whereClaues, whereArgs);
參數(shù)
table:數(shù)據(jù)庫(kù)的表名
whereClause:where 子句,比如:_id=?
whereArgs:where 子句中?的值
五、修改數(shù)據(jù)
1、execSQL(String sql)
2、使用對(duì)象的 delete 方法
ContentValues values = new ContentValues();
values.put(USERNAME, user.getUsername());
values.put(PASSWORD, user.getPassword());
String whereClaues="_id=?";
String [] whereArgs={String.valueOf(user.getId())};
db.update(TABLE_NAME, values, whereClaues, whereArgs);
參數(shù)
table:數(shù)據(jù)庫(kù)的表名
values:代表要修改的值,修改方法還是 put(key,values)
whereClause:條件子句,比如 id=?,name=?
whereArgs:為 whereClause 中的?賦值,比如:new String[]{"1","張三"}
圖:
參考代碼:
程序內(nèi)使用SQLite數(shù)據(jù)庫(kù)是通過SQLiteOpenHelper進(jìn)行操作
1.???????自己寫個(gè)類繼承SQLiteOpenHelper,重寫以下3個(gè)方法
public?void?onCreate(SQLiteDatabase?db)
{//創(chuàng)建數(shù)據(jù)庫(kù)時(shí)的操作,如建表}
public?void?onUpgrade(SQLiteDatabase?db,?int?oldVersion,?int?newVersion)
{
//版本更新的操作
}
2.????通過SQLiteOpenHelper的getWritableDatabase()獲得一個(gè)SQLiteDatabase數(shù)據(jù)庫(kù),以后的操作都是對(duì)SQLiteDatabase進(jìn)行操作。
3.???????對(duì)得到的SQLiteDatabase對(duì)象進(jìn)行增,改,刪,查等操作。
代碼
package?cx.myNote;
import?android.content.ContentValues;
import?android.content.Context;
import?android.content.Intent;
import?android.database.Cursor;
import?android.database.sqlite.SQLiteDatabase;
import?android.database.sqlite.SQLiteOpenHelper;
//DBOptions?for?login
public?class?DBOptions?{
private?static?final?String?DB_NAME?=?"notes.db";
private?static?final?String?DB_CREATE="create?table?logininf(name?text,pwd?text)";
public?class?DBHelper?extends?SQLiteOpenHelper
{
public?DBHelper(Context?context)?{
super(context,DB_NAME,?null,?1);
}
@Override
public?void?onCreate(SQLiteDatabase?db)?{
//?TODO?Auto-generated?method?stub
//建表
db.execSQL(DB_CREATE);
}
@Override
public?void?onUpgrade(SQLiteDatabase?db,?int?oldVersion,?int?newVersion)?{
//?TODO?Auto-generated?method?stub
db.execSQL("drop?table?if?exists?logininf");
onCreate(db);
}
}
private?Context?context;
private?SQLiteDatabase?db;
private?DBHelper?dbHelper;
public??DBOptions(Context?context)
{
this.context?=?context;
dbHelper?=?new?DBHelper(context);
db=dbHelper.getReadableDatabase();
}
//自己寫的方法,對(duì)數(shù)據(jù)庫(kù)進(jìn)行操作
public?String?getName()
{
Cursor?cursor?=?db.rawQuery("select?name?from?logininf",?null);
cursor.moveToFirst();
return?cursor.getString(0);
}
public?int?changePWD(String?oldP,String?pwd)
{
ContentValues?values?=?new?ContentValues();
values.put("pwd",?pwd);
return?db.update("logininf",?values,"pwd="+oldP,?null);
}
}
insert方法插入的一行記錄使用ContentValus存放,ContentValues類似于Map,它提供了put(String key, Xxx value)(其中key為數(shù)據(jù)列的列名)方法用于存入數(shù)據(jù)、getAsXxxx(String key)方法用于取出數(shù)據(jù)
一、新建外部SQLite數(shù)據(jù)庫(kù)
(1)下載并安裝 SQLite可視化管理工具(SQLite Expert Pro) v3.4.17 破解版
(2)將你手頭上的數(shù)據(jù)放到EXCEL表格中,保存為CSV格式的數(shù)據(jù)
(3)在此工具中按照你現(xiàn)有的數(shù)據(jù)格式新建數(shù)據(jù)庫(kù)和表,如數(shù)據(jù)庫(kù)為:contact.db,表為employee
(4)通過此工具菜單欄中Import/Export下的Import text file(CSV,TSC)功能,將你現(xiàn)有的CSV數(shù)據(jù)導(dǎo)入到你新建的數(shù)據(jù)表中(主要目的是省的一個(gè)一個(gè)的錄入了)
二、在eclipse中新建一個(gè)android app工程,并在新建的工程文件夾點(diǎn)右鍵new-folder,在res文件夾下新建raw文件夾(如果有就不用新建了)
三、用鼠標(biāo)將新建的SQLite數(shù)據(jù)庫(kù)文件contact.db拖動(dòng)到新建工程的res下的raw文件下,出現(xiàn)提示,選擇copy
四、程序代碼
private static final String DATABASE_PATH = "/data/data/你的主程序包路徑(如:com.szair.contact)/databases";
private static final int DATABASE_VERSION = 0;
private static final String DATABASE_NAME = "contact.db";
private static String outFileName = DATABASE_PATH + "/" + DATABASE_NAME;
try {
buildDatabase();//見下
} catch (Exception e) {
e.printStackTrace();
}
//SQLiteDatabase對(duì)象
SQLiteDatabase db=SQLiteDatabase.openDatabase(outFileName, null,SQLiteDatabase.NO_LOCALIZED_COLLATORS);
String t="SELECT 字段名1,字段名2 FROM employee WHERE **** ORDER BY ***";
Cursor c =db.rawQuery(t, null);
if(c.moveToFirst()){
for(int i=0;i
{
String ziduan1=c.getString(0);//字段1的數(shù)據(jù)
String ziduan2=c.getString(1);//字段1的數(shù)據(jù)
}
}
------------------------------------------------
//前面用到的buildDatabase方法
private void buildDatabase() throws Exception{
InputStream myInput = getResources().openRawResource(R.raw.sz_contact);
File file = new File(outFileName);
File dir = new File(DATABASE_PATH);
if (!dir.exists()) {
if (!dir.mkdir()) {
throw new Exception("創(chuàng)建失敗");
}
}
if (!file.exists()) {
try {
OutputStream myOutput = new FileOutputStream(outFileName);
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))0){
myOutput.write(buffer, 0, length);
}
myOutput.close();
myInput.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
五、程序發(fā)布
按照以上方式,可以將外部建的SQLite數(shù)據(jù)庫(kù)成功的發(fā)布出來
網(wǎng)站題目:android添加數(shù)據(jù),android添加數(shù)據(jù)庫(kù)文件
鏈接地址:http://m.rwnh.cn/article38/dscoesp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站設(shè)計(jì)、標(biāo)簽優(yōu)化、電子商務(wù)、面包屑導(dǎo)航、定制網(wǎng)站、虛擬主機(jī)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)