SQlite數據庫
|
編寫類繼承SQLiteOpenHelper
publicMySqlOpenHelper(Contextcontext){
//上下文數據庫名結果集(遊標)數據庫版本
super(context,"sqlite.db",null,1);
}
MySqlOpenHelper helper=new MySqlOpenHelper(getApplicationContext());
//打開或者創建一個數據庫。第一次就是創建
SQLiteDatabasedatabase1=helper.getWritableDatabase();
//如果磁盤滿了,返回一個只讀的
SQLiteDatabasedatabase2=helper.getReadableDatabase();
@Override
public void onCreate(SQLiteDatabase db) {
//數據庫底層全是以string存,來提升性能;
db.execSQL("create table info(_id integer primary key autoincrement,name varchar(20))");
}
版本跟換之後會執行適合做更新表的結構
增
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name", "dsy");
values.put("phone", "256521");
long l=db.insert("info", null, values);
db.close();
|
刪
SQLiteDatabase db=helper.getReadableDatabase();
//返回值代表影響的行數
int delete=db.delete("info", "name=?", new String[]{"dsy"});
db.close();
|
查
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("phone", "123");
int update=db.update("info", values, "name=?", new String[]{"dsy"});
db.close();
|
改
SQLiteDatabase db=helper.getWritableDatabase();
//第二個參數null表示查詢所有
Cursor cursor=db.query("info", new String[]{"phone"}, "name=?", new String[]{"dsy"}, null, null, null);
if(cursor!=null&&cursor.getCount()>0){
while(cursor.moveToNext()){
System.out.println(cursor.getString(0));
}
}
db.close();
|
SQLiteDatabase db=helper.getWritableDatabase();
//第二個參數null表示查詢所有
Cursor cursor=db.query("info", new String[]{"phone"}, "name=?", new String[]{"dsy"}, null, null, null);
if(cursor!=null&&cursor.getCount()>0){
while(cursor.moveToNext()){
System.out.println(cursor.getString(0));
}
}
db.close();
|
public void zz(View v){
db=helper.getWritableDatabase();
db.beginTransaction();
try {
db.execSQL("update count set money=money+100 where name=?",new Object[]{"dsy"});
db.execSQL("update count set money=money-100 where name=?",new Object[]{"scy"});
db.setTransactionSuccessful();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "服務器忙請稍後!", 1).show();
}finally{
db.endTransaction();
}
}
|
- 數據庫事物操作
- 創建的數據庫位於項目包名下的databases/
- 在命令行進入目錄,使用sqlite3 數據庫名字來查詢
- 增刪查改操作可以使用傳統的語句操作,也可以使用一下API操作
- 創建數據庫onUpgrade
- 創建數據庫onCreate方法合適做表結構的初始化
- 創建需要寫帶Context的參數的構造方法
- 使用Sqlite
|
LitePal2.0數據庫
|
Android studio 需要在build.grade中修改一下配置,使用Eclipse需要導入一下jar包
<https://github.com/LitePalFramework/LitePal/blob/master/downloads/litepal-2.0.0.jar>
dependencies {
implementation 'org.litepal.android:core:2.0.0'
}
|
- 在app/src/main 下新建assets文件夾,在文件夾中新建litepal.xml
<litepal>
<dbname value=""></dbname>
<version value="1"></version>
<list>
<mapping class="org.litepal.litepalsample.model.Book" />
</list>
</litepal>
|
- 編寫實體類Book繼承LitePalSupport作爲映射
- 在maniFest的application中加入
android:name="org.litepal.LitePalApplication"
- 在Activity中直接調用Connector.getDatabase();
- 增刪查改實體類需要繼承LitePalSupport
增
對象.setXx()
對象.save()
|
刪
LitePalSupport.deleteAll(指定表名和約束);
|
查
LitePalSupport.findAll(指定表名和約束);
findFirst
findLast
LitePalSupport.select("name","price").find(指定表名); //查詢指定列
LitePalSupport.where("page>?","200").find(指定表名);
LitePalSupport.order("price desc").find(指定表名);
LitePalSupport.limit(3).find(指定表名);
LitePalSupport.limit(3).offset(1).find(指定表名);
LitePalSupport.findBySQL("sql語句");
|
改
常規對象.set
- 對象.save()
- 對象.updateAll("name=? And author=?","a","b")
- 對象.setToDefault(“列名”) 對象.updateAll() 用來把字段值更新成默認值
|
|
ListView的基本用法
|
優化1:應爲ListView 快速拖動會導致內存溢出 原因是應爲消耗的內存大於釋放的內存,所以需要使用緩存機制
@Override
public View getView(finalint position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item,null);
holder = new ViewHolder();
/*得到各個控件的對象*/
holder.title = (TextView) convertView.findViewById(R.id.ItemTitle);
holder.text = (TextView) convertView.findViewById(R.id.ItemText);
holder.bt = (Button) convertView.findViewById(R.id.ItemButton);
convertView.setTag(holder);//綁定ViewHolder對象
}else{
holder = (ViewHolder)convertView.getTag();//取出ViewHolder對象
}
/*設置TextView顯示的內容,即我們存放在動態數組中的數據*/
holder.title.setText(getDate().get(position).get("ItemTitle").toString());
holder.text.setText(getDate().get(position).get("ItemText").toString());
holder.bt.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View v) {
});
return convertView;
}
|
優化2:ListView 的Layout_height需要使用 match-parent
使用wrap-content需要校驗多次,運行更多次的getView()方法,效率就差了
- 設置監聽 setOnItemClickListener
- 新建類繼承BaseAdapter,實現構造函數 getCount getItem getItemId getView
- 如何使用
- 在佈局加入ListView控件
- 在Activity中初始化
- 設置數據
- lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strs));
- SimpleAdapter mSimpleAdapter = new SimpleAdapter(this,listItem,new String[] {"ItemImage","ItemTitle", "ItemText"},
|
inflater對象的獲取三種方式
|
View view=LayoutInflater.from(getApplicationContext()).inflate(R.layout.item,null);
View view=View.inflate(getApplicationContext(), R.layout.item, null);
View View=getSystemService(LAYOUT_INFLATER_SERVICE).inflate(R.layout.item,null);
|