安卓初級教程(1):@Database(1)

时间:2021-07-12 12:47:40
 package com.example.android.db01;

 import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.AdapterView.OnItemSelectedListener; public class SQLiteTest extends Activity {
OnClickListener listener_add = null;
OnClickListener listener_update = null;
OnClickListener listener_delete = null;
OnClickListener listener_clear = null;
Button button_add;
Button button_update;
Button button_delete;
Button button_clear;
DBConnection helper;
public int id_this;
public interface UserSchema {
String TABLE_NAME = "Users"; //Table Name
String ID = "_id"; //ID
String USER_NAME = "user_name"; //User Name
String ADDRESS = "address"; //Address
String TELEPHONE = "telephone"; //Phone Number
String MAIL_ADDRESS = "mail_address"; //Mail Address
}
/** Called when the activity is first created. */
//SQLiteTest
@Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final EditText mEditText01 = (EditText)findViewById(R.id.EditText01);
final EditText mEditText02 = (EditText)findViewById(R.id.EditText02);
final EditText mEditText03 = (EditText)findViewById(R.id.EditText03);
final EditText mEditText04 = (EditText)findViewById(R.id.EditText04); helper = new DBConnection(this);
final SQLiteDatabase db = helper.getWritableDatabase();
final String[] FROM =
{
UserSchema.ID,
UserSchema.USER_NAME,
UserSchema.TELEPHONE,
UserSchema.ADDRESS,
UserSchema.MAIL_ADDRESS
};
//基本演算法,計算出db數數庫中總共有多少項數據列。
Cursor c = db.query(UserSchema.TABLE_NAME, new String[] {UserSchema.USER_NAME}, null, null, null, null, null);
c.moveToFirst();
CharSequence[] list = new CharSequence[c.getCount()];
for (int i = 0; i < list.length; i++) {
list[i] = c.getString(0);
c.moveToNext();
}
c.close();
//
Spinner spinner = (Spinner)findViewById(R.id.Spinner01);
spinner.setAdapter(new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, list)); spinner.setOnItemSelectedListener(new OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String user_name = ((Spinner)parent).getSelectedItem().toString();
Cursor c = db.query("Users", FROM , "user_name='" + user_name + "'", null, null, null, null);
c.moveToFirst();
id_this = Integer.parseInt(c.getString(0));
String user_name_this = c.getString(1);
String telephone_this = c.getString(2);
String address_this = c.getString(3);
String mail_address_this = c.getString(4);
c.close();
mEditText01.setText(user_name_this);
mEditText02.setText(telephone_this);
mEditText03.setText(address_this);
mEditText04.setText(mail_address_this);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
//增加資料,定義項是ContentValues,獲取資料時使用put,然後用SQLiteDatanase的方法去開啟數據庫,最後將資料輸入database,用的方法是db.insert
listener_add = new OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(UserSchema.USER_NAME, mEditText01.getText().toString());
values.put(UserSchema.TELEPHONE, mEditText02.getText().toString());
values.put(UserSchema.ADDRESS, mEditText03.getText().toString());
values.put(UserSchema.MAIL_ADDRESS, mEditText04.getText().toString());
SQLiteDatabase db = helper.getWritableDatabase();
db.insert(UserSchema.TABLE_NAME, null, values);
db.close();
onCreate(savedInstanceState);
}
};
//更新資料,定義項是ContentValues,獲取資料時使用put,然後用SQLiteDatanase的方法去開啟數據庫,最後將資料輸入database,用的方法是db.update
listener_update = new OnClickListener() {
public void onClick(View v) {
ContentValues values = new ContentValues();
values.put(UserSchema.USER_NAME, mEditText01.getText().toString());
values.put(UserSchema.TELEPHONE, mEditText02.getText().toString());
values.put(UserSchema.ADDRESS, mEditText03.getText().toString());
values.put(UserSchema.MAIL_ADDRESS, mEditText04.getText().toString());
String where = UserSchema.ID + " = " + id_this;
SQLiteDatabase db = helper.getWritableDatabase();
db.update(UserSchema.TABLE_NAME, values, where ,null);
db.close();
onCreate(savedInstanceState);
}
};
//[Delete]
listener_delete = new OnClickListener() {
public void onClick(View v) {
String where = UserSchema.ID + " = " + id_this;
SQLiteDatabase db = helper.getWritableDatabase();
db.delete(UserSchema.TABLE_NAME, where ,null);
db.close();
onCreate(savedInstanceState);
}
};
//[Clear]
listener_clear = new OnClickListener() {
public void onClick(View v) {
mEditText01.setText("");
mEditText02.setText("");
mEditText03.setText("");
mEditText04.setText("");
}
};
//BUTTON0i,i=1,2,3,4 OnClickListener
button_add = (Button)findViewById(R.id.Button01);
button_add.setOnClickListener(listener_add);
button_update = (Button)findViewById(R.id.Button02);
button_update.setOnClickListener(listener_update);
button_delete = (Button)findViewById(R.id.Button03);
button_delete.setOnClickListener(listener_delete);
button_clear = (Button)findViewById(R.id.Button04);
button_clear.setOnClickListener(listener_clear);
}
//SQLiteOpenHelper,創建資料庫
public static class DBConnection extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "PhoneBookDB";
private static final int DATABASE_VERSION = 1;
private DBConnection(Context ctx) {
super(ctx, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + UserSchema.TABLE_NAME + " ("
+ UserSchema.ID + " INTEGER primary key autoincrement, "
+ UserSchema.USER_NAME + " text not null, "
+ UserSchema.TELEPHONE + " text not null, "
+ UserSchema.ADDRESS + " text not null, "
+ UserSchema.MAIL_ADDRESS + " text not null "+ ");";
//Log.i("haiyang:createDB=", sql);
db.execSQL(sql);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
}

安卓初級教程(1):@Database(1)的更多相关文章

  1. 安卓初級教程(4):sqlite建立資料庫

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...

  2. 安卓初級教程(3):ContentProvider的運用原理

    package com.example.android.provider; import java.util.ArrayList; import java.util.HashMap; import j ...

  3. 安卓初級教程(5):TabHost的思考

    package com.myhost; import android.os.Bundle; import android.view.LayoutInflater; import android.wid ...

  4. 安卓初級教程(2):SD創建file,儲存與讀寫的方法&lpar;1&rpar;

    package com.sdmadik; import java.io.*; import android.app.Activity; import android.os.Bundle; import ...

  5. 安卓中級教程(3):ScrollView

    以上是scrollview的圖例,可見srollview是一種滑動功能的控件,亦是非常常見的控件. 一般寫法如下: package com.mycompany.viewscroller; import ...

  6. 安卓中級教程(10):&commat;InjectView

    package com.example.android.db01; import android.app.Activity; import android.content.ContentValues; ...

  7. 安卓中級教程(6):annotation的基本用法

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

  8. 安卓中級教程(1):&commat;InjectView

    package com.mycompany.hungry; import android.annotation.SuppressLint; import android.app.Activity; i ...

  9. 安卓中級教程(11):深入研究餓了麼的各個java檔運作關係(1)

    package com.example.ele_me.activity; import android.annotation.SuppressLint; import android.app.Acti ...

随机推荐

  1. 如何使用免费PDF控件从PDF文档中提取文本和图片

             如何使用免费PDF控件从PDF文档中提取文本和图片 概要 现在手头的项目有一个需求是从PDF文档中提取文本和图片,我以前也使用过像iTextSharp, PDFBox 这些免费的PD ...

  2. iOS 常用的向上&comma;向下取整&comma; 四舍五入函数

    向上取整:ceil(x),返回不小于x的最小整数; 向下取整:floor(x),返回不大于x的最大整数; 四舍五入:round(x) 截尾取整函数:trunc(x)  

  3. weblogic部署项目包,报空指针错误

    贴出 报错代码 <weblogic> <> <> <1479765377228> <BEA-240003> <Console enco ...

  4. 使用AIDL将接口暴露给客户端&lpar;远程绑定Service&rpar;

    import java.util.Timer;import java.util.TimerTask; import jww.mediaprovidertest.ICat.Stub;import and ...

  5. Linux命令之env:显示当前用户的环境变量

    Linux系统里的env命令可以显示当前用户的环境变量,还可以用来在指定环境变量下执行其他命令.下面来比较一下set,env和export命令的异同:set命令显示当前shell的变量,包括当前用户的 ...

  6. v&dollar;lock 视图访问慢解决方法

    V$ 视图访问慢 --解决方法 分析:可能是有数据字典统计信息过久,造成. exec dbms_stats.gather_fixed_objects_stats; ------收集所有数据字典的fix ...

  7. 【Codeforces Round 1114】Codeforces &num;538 &lpar;Div&period; 2&rpar;

    Codeforces Round 1114 这场比赛做了\(A\).\(C\).\(D\).\(E\),排名\(134\). \(B\)题做了很长时间,好不容易最后一分钟\(Pretest\ Pass ...

  8. 自学Zabbix11&period;3 Zabbix SNMP 常用OID列表

    点击返回:自学Zabbix之路点击返回:自学Zabbix4.0之路点击返回:自学zabbix集锦 自学Zabbix11.3 Zabbix SNMP 常用OID列表 点击获取CISCO设备OID 系统参 ...

  9. 事件冒泡及事件委托的理解(JQuery Dom操作)

    jQuery事件冒泡: click mouseenter 等事件没有绑定 也会触发,只是触发后没有任何结果 子元素触发事件后,会把触发事件传递给父元素,那么父元素也会被触发. 不管有没有绑定事件,都会 ...

  10. js生成hash序列

    炒鸡简单的js生成hash序列的方法.如下: function createHash (hashLength) { if (!hashLength || typeof(Number(hashLengt ...