編輯:關於android開發
主java
package com.itheima.crud; import android.app.Activity; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import com.itheima.crud.R; import com.itheima.crud.bean.InfoBean; import com.itheima.crud.dao.InfoDao; public class MainActivity extends Activity implements OnClickListener { private Context mContext; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; //創建一個幫助類對象 MySqliteOpenHelper mySqliteOpenHelper = new MySqliteOpenHelper(mContext); //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //找到相應控件 findViewById(R.id.bt_add).setOnClickListener(this); findViewById(R.id.bt_del).setOnClickListener(this); findViewById(R.id.bt_update).setOnClickListener(this); findViewById(R.id.bt_query).setOnClickListener(this); } @Override public void onClick(View v) { InfoDao infoDao = new InfoDao(mContext);//創建一個dao對象做增刪改查 switch (v.getId()) { case R.id.bt_add: InfoBean bean = new InfoBean(); bean.name = "張三"; bean.phone ="110"; infoDao.add(bean); InfoBean bean1 = new InfoBean(); bean1.name = "李四"; bean1.phone ="120"; infoDao.add(bean1); break; case R.id.bt_del: infoDao.del("張三"); break; case R.id.bt_update: InfoBean bean2 = new InfoBean(); bean2.name = "張三"; bean2.phone ="119"; infoDao.update(bean2); break; case R.id.bt_query: infoDao.query("張三"); infoDao.query("李四"); break; default: break; } } }
與主java同一包下的類 繼承與數據庫的那個類
package com.itheima.crud; import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class MySqliteOpenHelper extends SQLiteOpenHelper { public MySqliteOpenHelper(Context context) { //context :上下文 , name:數據庫文件的名稱 factory:用來創建cursor對象,默認為null //version:數據庫的版本號,從1開始,如果發生改變,onUpgrade方法將會調用,4.0之後只能升不能將 super(context, "info.db", null,1); } //oncreate方法是數據庫第一次創建的時候會被調用; 特別適合做表結構的初始化,需要執行sql語句;SQLiteDatabase db可以用來執行sql語句 @Override public void onCreate(SQLiteDatabase db) { //通過SQLiteDatabase執行一個創建表的sql語句 db.execSQL("create table info (_id integer primary key autoincrement,name varchar(20),phone varchar(11))"); } //onUpgrade數據庫版本號發生改變時才會執行; 特別適合做表結構的修改 @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { //添加一個phone字段 // db.execSQL("alter table info add phone varchar(11)"); } }
新建包--新建類---用來增刪改查
package com.itheima.crud.dao; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.itheima.crud.MySqliteOpenHelper; import com.itheima.crud.bean.InfoBean; public class InfoDao { private MySqliteOpenHelper mySqliteOpenHelper; public InfoDao(Context context){ //創建一個幫助類對象 mySqliteOpenHelper = new MySqliteOpenHelper(context); } public void add(InfoBean bean){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getWritableDatabase(); //sql:sql語句, bindArgs:sql語句中占位符的值 db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone}); //關閉數據庫對象 db.close(); } public void del(String name){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //sql:sql語句, bindArgs:sql語句中占位符的值 db.execSQL("delete from info where name=?;", new Object[]{name}); //關閉數據庫對象 db.close(); } public void update(InfoBean bean){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //sql:sql語句, bindArgs:sql語句中占位符的值 db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name}); //關閉數據庫對象 db.close(); } public void query(String name){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //sql:sql語句, selectionArgs:查詢條件占位符的值,返回一個cursor對象 Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?;", new String []{name}); //解析Cursor中的數據 if(cursor != null && cursor.getCount() >0){//判斷cursor中是否存在數據 //循環遍歷結果集,獲取每一行的內容 while(cursor.moveToNext()){//條件,游標能否定位到下一行 //獲取數據 int id = cursor.getInt(0); String name_str = cursor.getString(1); String phone = cursor.getString(2); System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone); } cursor.close();//關閉結果集 } //關閉數據庫對象 db.close(); } }
名字 電話信息包 裡面就是一個類
package com.itheima.crud.dao; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.itheima.crud.MySqliteOpenHelper; import com.itheima.crud.bean.InfoBean; public class InfoDao { private MySqliteOpenHelper mySqliteOpenHelper; public InfoDao(Context context){ //創建一個幫助類對象 mySqliteOpenHelper = new MySqliteOpenHelper(context); } public void add(InfoBean bean){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getWritableDatabase(); //sql:sql語句, bindArgs:sql語句中占位符的值 db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone}); //關閉數據庫對象 db.close(); } public void del(String name){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //sql:sql語句, bindArgs:sql語句中占位符的值 db.execSQL("delete from info where name=?;", new Object[]{name}); //關閉數據庫對象 db.close(); } public void update(InfoBean bean){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //sql:sql語句, bindArgs:sql語句中占位符的值 db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name}); //關閉數據庫對象 db.close(); } public void query(String name){ //執行sql語句需要sqliteDatabase對象 //調用getReadableDatabase方法,來初始化數據庫的創建 SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase(); //sql:sql語句, selectionArgs:查詢條件占位符的值,返回一個cursor對象 Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?;", new String []{name}); //解析Cursor中的數據 if(cursor != null && cursor.getCount() >0){//判斷cursor中是否存在數據 //循環遍歷結果集,獲取每一行的內容 while(cursor.moveToNext()){//條件,游標能否定位到下一行 //獲取數據 int id = cursor.getInt(0); String name_str = cursor.getString(1); String phone = cursor.getString(2); System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone); } cursor.close();//關閉結果集 } //關閉數據庫對象 db.close(); } }
4個按鈕 xml設計
<?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" android:orientation="vertical" android:paddingTop="@dimen/activity_vertical_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingBottom="@dimen/activity_vertical_margin" android:layout_height="match_parent" android:layout_width="match_parent" xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android"> <Button android:id="@+id/bt_add" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/add"/> <Button android:id="@+id/bt_del" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/del"/> <Button android:id="@+id/bt_update" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/update"/> <Button android:id="@+id/bt_query" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="@string/query"/> </LinearLayout>
老師筆記
# 2 Android下數據庫第一種方式增刪改查
1.創建一個幫助類的對象,調用getReadableDatabase方法,返回一個SqliteDatebase對象
2.使用SqliteDatebase對象調用execSql()做增刪改,調用rawQuery方法做查詢。
******特點:增刪改沒有返回值,不能判斷sql語句是否執行成功。sql語句手動寫,容易寫錯
private MySqliteOpenHelper mySqliteOpenHelper;
public InfoDao(Context context){
//創建一個幫助類對象
mySqliteOpenHelper = new MySqliteOpenHelper(context);
}
public void add(InfoBean bean){
//執行sql語句需要sqliteDatabase對象
//調用getReadableDatabase方法,來初始化數據庫的創建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql語句, bindArgs:sql語句中占位符的值
db.execSQL("insert into info(name,phone) values(?,?);", new Object[]{bean.name,bean.phone});
//關閉數據庫對象
db.close();
}
public void del(String name){
//執行sql語句需要sqliteDatabase對象
//調用getReadableDatabase方法,來初始化數據庫的創建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql語句, bindArgs:sql語句中占位符的值
db.execSQL("delete from info where name=?;", new Object[]{name});
//關閉數據庫對象
db.close();
}
public void update(InfoBean bean){
//執行sql語句需要sqliteDatabase對象
//調用getReadableDatabase方法,來初始化數據庫的創建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql語句, bindArgs:sql語句中占位符的值
db.execSQL("update info set phone=? where name=?;", new Object[]{bean.phone,bean.name});
//關閉數據庫對象
db.close();
}
public void query(String name){
//執行sql語句需要sqliteDatabase對象
//調用getReadableDatabase方法,來初始化數據庫的創建
SQLiteDatabase db = mySqliteOpenHelper.getReadableDatabase();
//sql:sql語句, selectionArgs:查詢條件占位符的值,返回一個cursor對象
Cursor cursor = db.rawQuery("select _id, name,phone from info where name = ?", new String []{name});
//解析Cursor中的數據
if(cursor != null && cursor.getCount() >0){//判斷cursor中是否存在數據
//循環遍歷結果集,獲取每一行的內容
while(cursor.moveToNext()){//條件,游標能否定位到下一行
//獲取數據
int id = cursor.getInt(0);
String name_str = cursor.getString(1);
String phone = cursor.getString(2);
System.out.println("_id:"+id+";name:"+name_str+";phone:"+phone);
}
cursor.close();//關閉結果集
}
//關閉數據庫對象
db.close();
}
手機影音2--軟件架構分析,影音2--架構分析 1.標題欄 <?xml version=1.0 encoding=utf-8?> <com.atgui
Android快捷方式解密 Android快捷方式解密 Android快捷方式作為Android設備的殺手锏技能,一直都是非常重要的一個功能,也正是如此,各種流氓Ap
【原創】StickHeaderListView的簡單實現,解決footerView問題,stickheaderlistview1、前言: 前幾天用了GitH
Android開發:Android SDK的介紹 1.Android SDK 即Android Software Development Kit.它提