這個月的17日,就要去大連比賽了,是今年的職業技能大賽,整理了一下本身過去寫過的代碼,這個是Android數據庫的知識,涉及數據庫的增刪改查,代碼很普通,稍有Android基礎的,估計都能看懂,無邪本着互聯網精神,但願你們一塊兒進步,大神直接飄過,勿噴。。。 android
主界面的代碼: sql
package com.example.sqlitetestdemo; 數據庫
import android.os.Bundle;
import android.app.Activity;
import
android.content.ContentValues;
import android.content.Intent;
import
android.database.Cursor;
import
android.database.sqlite.SQLiteDatabase;
import
android.support.v4.widget.SimpleCursorAdapter;
import
android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import android.widget.Button;
import
android.widget.ListView;
import android.widget.SimpleAdapter; app
public class MainActivity extends Activity {
private Button
skip;
private ListView list;
private SQLiteDatabase
mydb=null;
private Cursor cursor;
public static String
Name=null;
public static String Text=null;
public static String
DataBase_Name="StudentDataBase.db";
public static String
Table_Name="Student";
public static String Create_Table="create table if
not exists "+Table_Name+"(_id integer primary key autoincrement
,name,text)";
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
//建立數據庫
mydb=openOrCreateDatabase(DataBase_Name,
MODE_PRIVATE, null);
mydb.execSQL(Create_Table);//建立表
ContentValues
values=new ContentValues();
values.put("name",
"王曉讓");
values.put("text", "2011237144 男
");
mydb.insert(Table_Name, null,
values);//插入數據
//在ListView裏顯示數據
this.showText();
//skip按鈕的監聽事件
skip=(Button)
findViewById(R.id.saveto);
skip.setOnClickListener(new OnClickListener()
{
public void onClick(View v) {
Intent intent=new
Intent(MainActivity.this,MySQLite.class);
startActivityForResult(intent,
6);
}
});
}
protected void
onActivityResult(int requestCode, int resultCode, Intent data) {
switch(
resultCode){
case
RESULT_OK:
cursor.requery();
//重繪
list.invalidateViews();
break;
default:
break;
}
super.onActivityResult(requestCode,
resultCode, data);
} 佈局
public void showText(){
//獲取數據庫的全部數據
cursor=mydb.query(Table_Name,new String[]{"_id","name","text"}, null,null, null,
null,null);
list=(ListView) findViewById(R.id.listView1);
SimpleCursorAdapter simple=new SimpleCursorAdapter(this, R.layout.show, cursor,
new String[]{"name","text"}, new
int[]{R.id.textView1,R.id.textView2});
//綁定適配器
list.setAdapter(simple);
}
public
boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds
items to the action bar if it is
present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return
true;
} this
} spa
Android數據庫操做的代碼: sqlite
package com.example.sqlitetestdemo; xml
import android.app.Activity;
import
android.content.ContentValues;
import
android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import
android.view.View;
import android.view.View.OnClickListener;
import
android.widget.Button;
import android.widget.EditText; 事件
public class MySQLite extends Activity{
private SQLiteDatabase
mydb=null;
private Button add;
private Button modify;
private
Button delete;
private EditText name;
private EditText
content;
private static String DataBase_Name="StudentDataBase.db";
private static String Table_Name="Student";
private static String
Name="name";
private static String Text="text";
private static
String Create_Table="create table if not exists "+Table_Name+ "(_id integer
primary key autoincrement ,name,text)";
protected void onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.context_edit);
//初始化控件
this.init();
}
//初始化控件
public
void init(){
name=(EditText)
findViewById(R.id.name);
content=(EditText)
findViewById(R.id.content);
add=(Button)
findViewById(R.id.button1);
modify=(Button)
findViewById(R.id.button2);
delete=(Button)
findViewById(R.id.button3);
add.setOnClickListener(new
ButtonListener());
modify.setOnClickListener(new
ButtonListener());
delete.setOnClickListener(new
ButtonListener());
}
//按鈕的監聽事件
class ButtonListener implements
OnClickListener{
public void onClick(View v)
{
switch(v.getId()){
case
R.id.button1:
//打開數據庫
mydb=openOrCreateDatabase(DataBase_Name,
MODE_PRIVATE, null);
ContentValues cv=new
ContentValues();
//存入鍵值對
cv.put(Name,name.getText().toString()
);
cv.put(Text,
content.getText().toString());
mydb.insert(Table_Name, null,
cv);//插入數據,若是不成功返回-1
//設置參數
setResult(RESULT_OK);
mydb.close();//關閉資源
//銷燬視圖
finish();
break;
case
R.id.button3:
//打開數據庫
mydb=openOrCreateDatabase(DataBase_Name,
MODE_PRIVATE, null);
String whereClause="name=?";
String[]
whereArgs={name.getText().toString()};
//刪除數據
mydb.delete(Table_Name,
whereClause,
whereArgs);
//設置參數
setResult(RESULT_OK);
mydb.close();//關閉資源
//銷燬視圖
finish();
break;
case
R.id.button2:
//打開數據庫
mydb=openOrCreateDatabase(DataBase_Name,
MODE_PRIVATE, null);
ContentValues values=new
ContentValues();
values.put(Name,
name.getText().toString());
values.put(Text,
content.getText().toString());
String
whereClause1="name=?";
String[]
whereArgs1={name.getText().toString()};
//修改數據
mydb.update(Table_Name,
values, whereClause1,
whereArgs1);
//設置參數
setResult(RESULT_OK);
mydb.close();//關閉資源
//銷燬視圖
finish();
break;
default:
break;
}
}
}
}
主界面的佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/saveto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="存儲" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
與數據庫操做相關聯的佈局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入姓名" />
<EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入內容" />
<EditText
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="增長" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="刪除" />
</LinearLayout>
</LinearLayout>
與ListView相關的佈局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名: "
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="詳細內容: "
/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
/>
</LinearLayout>
</LinearLayout>