編輯:關於android開發
首先要創建一個類,繼承自SQLiteOpenHelper,用來打開數據庫
package com.SQLiteTest2;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class MyOpenHelper extends SQLiteOpenHelper {
public static final String DB_NAME = "student";
public static final String TABLE_NAME = "student_information";
public static final String ID = "_id";
public static final String NUMBER = "number";
public static final String NAME = "name";
public MyOpenHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("create table if not exists "+TABLE_NAME+" ("
+ ID + " integer primary key,"
+ NUMBER + " varchar,"
+ NAME + " varchar)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
在onCreate()函數中,我創建一個表,用來存儲學生的學號和姓名信息
然後從UI上的TextEdit控件上獲取數據
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/enter"
/>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number"
/>
<EditText
android:id="@+id/number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="@+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
/>
<EditText
android:id="@+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></EditText>
</LinearLayout>
<Button
android:id="@+id/submit"
android:text="@string/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<TextView
android:id="@+id/IdLongitude"
android:textSize="16px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/IdLatitude"
android:textSize="16px"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
接下來就是主窗體的代碼部分了
import static com.SQLiteTest2.MyOpenHelper.*;
public class SQLiteTest2 extends Activity {
MyOpenHelper myHelper;
EditText [] textArray;
Button btn_save;
int [] textIds = {
R.id.number,
R.id.name
};
View.OnClickListener myListener = new View.OnClickListener() {
@Override
public void onClick(View v) { //保存按鈕按下觸發該事件
String [] strArray = new String[textArray.length];
for(int i = 0; i < strArray.length; i++){
strArray[i] = textArray[i].getText().toString().trim(); //獲得用戶輸入的
信息數組
}
insert(strArray);
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textArray = new EditText[textIds.length];
for(int i=0;i<textIds.length;i++){
textArray[i] = (EditText)findViewById(textIds[i]);
}
myHelper = new MyOpenHelper(this, MyOpenHelper.DB_NAME, null, 1);
btn_save = (Button) findViewById(R.id.submit);
btn_save.setOnClickListener(myListener);
}
public void insert(String [] strArray)
{
SQLiteDatabase db = myHelper.getWritableDatabase(); //獲得數據庫對象
ContentValues values = new ContentValues();
values.put(NUMBER, strArray[0]);
values.put(NAME, strArray[1]);
long count = db.insert(TABLE_NAME, ID, values); //插入數據
db.close();
if(count == -1){
Toast.makeText(this, "插入失敗!", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(this, "插入成功!", Toast.LENGTH_SHORT).show();
}
}
}
刪除記錄的函數
public void deleteContact(int id){
SQLiteDatabase db = myHelper.getWritableDatabase(); //獲得數據庫對象
db.delete(TABLE_NAME, ID+"=?", new String[]{id+""});
db.close();
}
The Genymotion Virtual device could not obtain an IP address解決辦法,genymotionobtain打開Ge
android 滾動條下拉反彈的效果(類似微信朋友圈) 微信朋友圈上面的圖片封面,QQ空間說說上面的圖片封面都有下拉反彈的效果,這些都是使用滾動條實現的。下拉,當松開
熱修復-Nuwa學習篇,熱修復-nuwanuwa熱修復是基於qq空間團隊的思路,最近的熱度話題了,很多種方案,自己先研究幾種方案,基本上都各有優勢,學習肯定得先挑個軟柿子
Android 手機衛士--綁定sim卡序列號,androidsim現在開始具體 處理每一個導航頁面的邏輯,首先看第二個導航頁 本文地址:http://www.cnbl