安卓數據庫——SQLite使用
1.建數據庫,數據庫適配器,寫增刪改查方法:
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;
public class MySQLiteOpenHelper extends SQLiteOpenHelper {
public MySQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.d(“msg”,“創建數據庫”);
String createStr = “create table student (id integer primary key autoincrement not null,” +
“name varchar not null,” +
“tell varchar not null,” +
“address varchar not null)”;
db.execSQL(createStr);
//插入學生信息
public void insert(String tableNm, Student student) {
SQLiteDatabase sql = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", student.getName());
values.put("tell", student.getTell());
values.put("address", student.getAddress() );
sql.insert(tableNm, null, values);
}
public void delete(String tableNm, int id) {
SQLiteDatabase sql = getWritableDatabase();
String where = "id=?";
sql.delete(tableNm, where, new String[]{id + ""});
}
public List selectAll(String tableNm) {
List students = new ArrayList<>();
SQLiteDatabase database = getReadableDatabase();
String selectstr = “select * from " + tableNm;
Cursor cursor = database.rawQuery(selectstr, null);
// Cursor cursor=database.query(tableNm,new String[]{“id”,“name”,“age”,“sex”},null,null,null,null,null);
cursor.moveToFirst();
do {
int id = cursor.getInt(cursor.getColumnIndex(“id”));
String name = cursor.getString(cursor.getColumnIndex(“name”));
String tell = cursor.getString(cursor.getColumnIndex(“tell”));
String address = cursor.getString(cursor.getColumnIndex(“address”));
students.add(new Student(id, name, tell, address));
}
while (cursor.moveToNext());
return students;
}
public void update(String tableNm,ContentValues values,int id)
{
SQLiteDatabase database = getWritableDatabase();
// String where = “id = ?”;
// database.update(tableNm,values,where,new String[]{id+”"});
// UPDATE newTable SET id = 0, name = ‘’, age = 0, sex = ‘’ WHERE id = ;
String sql = “update “+tableNm+” set name=?,tell=?,address=? where id = ?”;
database.execSQL(sql,new String[]{values.get(“name”).toString(),values.get(“tell”).toString(),values.get(“address”).toString(),id+""})
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
2.寫參數,建bean類
public class Student {
private int id;
private String name;
private String tell;
private String address;
public Student() {
}
public Student(String name, String tell, String address) {
this.name = name;
this.tell = tell;
this.address = address;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getTell() {
return tell;
}
public void setTell(String tell) {
this.tell = tell;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", tell='" + tell + '\'' +
", address='" + address + '\'' +
'}';
}
public Student(int id, String name, String tell, String address) {
this.id = id;
this.name = name;
this.tell = tell;
this.address = address;
}
}
3. mainactivity中的代碼
import android.content.ContentValues;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView lv;
private MySQLiteOpenHelper helper;
static String student_table = “student”;
Button ad_del,ad_xiu,main_add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//數據庫表名 ,版本號
helper = new MySQLiteOpenHelper(getApplicationContext(), “studentdb”, null, 1);
lv=findViewById(R.id.lv);
ad_xiu=findViewById(R.id.ad_xiu);
ad_del=findViewById(R.id.ad_del);
main_add=findViewById(R.id.main_add);
//查詢全部并顯示到listview中
List SJ=helper.selectAll(student_table);
lv.setAdapter(new lvAdapter(SJ));
//點擊跳轉
main_add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,ADDActivity.class);
startActivity(intent);
}
});
}
//listview適配器
class lvAdapter extends BaseAdapter {
private List students;
//傳參類型
public lvAdapter(List students) {
this.students = students;
}
@Override
public int getCount() {
return students.size();
}
@Override
public Object getItem(int position) {
return students.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) {
//把查詢數據填到listview 的item中
view = LayoutInflater.from(getApplicationContext()).inflate(R.layout.lv_layout, null);
Student student = students.get(position);
TextView txt = view.findViewById(R.id.ad_name);
txt.setText(student.getName());
txt = view.findViewById(R.id.ad_tell);
txt.setText(student.getTell());
txt = view.findViewById(R.id.ad_adress);
txt.setText(student.getAddress() + "");
} else {
view = convertView;
}
return view;
}
}
}
*4.main布局:
5.跳轉界面:
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
public class ADDActivity extends AppCompatActivity {
EditText add_name,add_tel,add_adress;
private MySQLiteOpenHelper helper;
static String student_table = “student”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
helper = new MySQLiteOpenHelper(getApplicationContext(), “studentdb”, null, 1);
add_name=findViewById(R.id.add_name);
add_tel=findViewById(R.id.add_tel);
add_adress=findViewById(R.id.add_address);
Button btn=findViewById(R.id.add_qd);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = add_name.getText().toString();
String tel=add_tel.getText().toString();
String address=add_adress.getText().toString();
helper.insert(“student”, new Student(name, tel, address));
Intent intent=new Intent(ADDActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
6.跳轉界面布局:
<?xml version="1.0" encoding="utf-8"?>
listview布局
<?xml version="1.0" encoding="utf-8"?><TextView
android:id="@+id/ad_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="邱國忠"
android:textSize="25dp"
/>
<TextView
android:id="@+id/ad_tell"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ad_name"
android:text="15770836531"
android:textSize="22dp"
android:layout_margin="2dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="學校"
android:layout_toRightOf="@id/ad_tell"
android:layout_marginRight="3dp"
android:layout_marginTop="5dp"
/>
<TextView
android:id="@+id/ad_adress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="紅紅火火恍恍惚惚哈哈"
android:textSize="20dp"
android:layout_below="@id/ad_name"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="設為默認"
android:layout_below="@id/ad_adress"/>
<Button
android:id="@+id/ad_xiu"
android:layout_width="55dp"
android:layout_height="35dp"
android:text="編輯"
android:layout_toLeftOf="@id/ad_del"
android:layout_below="@id/ad_adress"
/>
<Button
android:id="@+id/ad_del"
android:layout_width="55dp"
android:layout_height="35dp"
android:text="刪除"
android:layout_alignParentRight="true"
android:layout_below="@id/ad_adress"
/>
智能推薦
安卓之Afinal的使用(創建數據庫)
Afinal的介紹: Afinal是一個開源的android的orm和ioc應用開發框架,其特點是小巧靈活,代碼入侵量少。在android應用開發中,通過Afinal的ioc框架,諸如UI綁定,事件綁定,通過注解可以自動綁定。通過Afinal的orm框架,無需任何配置信息,一行代碼就可以對android的sqlite數據庫進行增刪改查操作。(引用大佬的講解) FinalDB模塊:android中的...
SQLite數據庫存儲(一)【安卓學習筆記】
對于MODE_PRIVATE,MODE_APPEND兩種模式,對寫文件的影響有什么不同? MODE_PRIVATE:該文件只能被當前程序讀寫,會把原來的內容覆蓋掉 MODE_APPEND:該文件的內容可追加,不會把原來的內容覆蓋掉,新寫的內容追加在文件后面 但是對于修改文件中的部分內容,應該怎么做呢?這就需要我們用到SQLite數據庫 主要內容: 創建和打開一個SQLite 數據庫 SQLite數...
sqlite數據庫使用
sqlite數據庫的使用 運行結果: sqlite數據庫需要的引用的庫如下: sqlite數據庫需要的引用的庫下載地址:https://download.csdn.net/download/LongtengGensSupreme/11985402 ...
安卓——數據篇-SQLite
http://tool.oschina.net/apidocs/apidoc?api=android/reference 詳細的API文檔 1.每個程序都有自己的數據庫,默認情況下互不干擾 2.在查看自己創建的數據庫時遇到了小插曲,打開的file explore時空白的,上網搜了一下,換了個API23的模擬器就ok了。 3.數據庫存盤 file explore那行右邊的兩個黑色按鈕,點擊靠左的那個...
HDU-安卓程序開發之讀寫資源文件/SQLite數據庫/數據共享
前言 這次為了做更好看的界面,更新了一下android studio到4.1.1 踩了很多坑,最后還是手機熱點+梯子下載安裝包,在線更新gradle弄好的,然后新建一個工程又出問題 (我和我的室友的問題不一樣,這里闡述一下我遇到的問題和解決方案https://blog.csdn.net/cshoney/article/details/90142447) 不多說了,開搞! 界面 讀寫資源文件 SQL...
猜你喜歡
freemarker + ItextRender 根據模板生成PDF文件
1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...