一,AS開發app用,所用的數據庫有限制,必須使用較小的SQLite(MySql和Sql Server想想就不顯示)
但是該數據庫並不需要我們單獨下載,安裝的SDK中已經有了,在C:\AndroidSDK\platform-tools目錄下
最下面一個exe文件就是,雙擊可以打開dos界面進行編寫
將上述講的SQLite的目錄(C:\AndroidSDK\platform-tools)添加到環境變量path下就可以完成配置
驗證是否完成配置:打開dos界面,輸入SQLite3,出現以下界面就表示成功了
二,分享一款SQLite3的可視化操作界面
黑框框雖然看起來逼格非常高,但是個人覺得太撈比了,主要是太麻煩了(個人很懶)
一個可視化界面的exe文件:SQLiteStudio 百度雲盤地址 http://pan.baidu.com/s/1dES3Zep
不用安裝,解壓后直接使用
三,以下分享一個在百度上找了很久的很小的對於數據庫增刪改查操作的實踐~~~
原文出處:http://www.jianshu.com/p/ee08f75b407c
<一>界面部分
我個人看法,開發app,首先可以把各個界面設計出來,這樣也就明確了自己想要實現什么功能,那么在后面的代碼實現過程中,就相應的有了目的性,界面不夠,我們再添加就好了~
首先上兩張圖,也就是這個小project中的兩個界面,也就是我們要實現的幾個功能
界面一:顯示所有學生名字,然后點某個人,跳到界面二
界面二:某個學生的具體信息,可以對這個學生進行刪改,或者添加新同學~
1,首先創建一個project,名字隨意
2,實現第一個頁面布局activity_main.xml(也就是頁面一)
(我個人直接把學習網站的代碼拔過來了,但是會有一些warning)
比如:android:text="Add"
最好在strings.xml中配置一下 :
添加一條 <string name="add">Add</string>
然后改成android:text = "@string/add"
並不是說第一種錯了,而是第二種規范性更棒,日后管理更新等操作更加方便,只需要在strings中改一下就ok了

1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingLeft="@dimen/activity_horizontal_margin" 6 android:paddingRight="@dimen/activity_horizontal_margin" 7 android:paddingTop="@dimen/activity_vertical_margin" 8 android:paddingBottom="@dimen/activity_vertical_margin" 9 tools:context=".MainActivity"> 10 11 <Button 12 android:layout_width="wrap_content" 13 android:layout_height="wrap_content" 14 android:text="@string/add" 15 android:id="@+id/btnAdd" 16 android:layout_alignParentBottom="true" 17 android:layout_alignParentLeft="true" 18 tools:ignore="RtlHardcoded" /> 19 <ListView 20 android:id="@android:id/list" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" 23 android:layout_above="@+id/btnAdd" 24 /> 25 26 <Button 27 android:layout_width="wrap_content" 28 android:layout_height="wrap_content" 29 android:text="@string/ListAll" 30 android:id="@+id/btnGetAll" 31 tools:ignore="RtlHardcoded" 32 android:layout_alignParentBottom="true" 33 android:layout_toEndOf="@+id/btnAdd" /> 34 </RelativeLayout>
3,接下來,畫出界面二
我們可以直接創建一個新的activity,因為沒個界面都要配合一個動作的嘛~,因此可以直接new一個新的activity,StudentDetail.java
其布局為第二個頁面activity_student_detail.xml

1 <?xml version="1.0" encoding="utf-8"?> 2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:id="@+id/activity_student_detail" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 android:paddingBottom="@dimen/activity_vertical_margin" 8 android:paddingLeft="@dimen/activity_horizontal_margin" 9 android:paddingRight="@dimen/activity_horizontal_margin" 10 android:paddingTop="@dimen/activity_vertical_margin" 11 tools:context="examples.ouc.com.sqlitedemo.StudentDetail"> 12 13 <TextView 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:text="@string/name" 17 android:id="@+id/tvName" 18 android:layout_alignParentTop="true" 19 android:layout_alignParentLeft="true" 20 android:layout_alignParentStart="true" 21 android:layout_marginTop="30dp" /> 22 23 <TextView 24 android:layout_width="wrap_content" 25 android:layout_height="wrap_content" 26 android:text="@string/email" 27 android:id="@+id/tvEmail" 28 android:layout_below="@id/tvName" 29 android:layout_alignParentLeft="true" 30 android:layout_alignParentStart="true" 31 android:layout_marginTop="30dp" /> 32 <TextView 33 android:layout_width="wrap_content" 34 android:layout_height="wrap_content" 35 android:text="@string/age" 36 android:id="@+id/tvAge" 37 android:layout_below="@id/tvEmail" 38 android:layout_alignParentLeft="true" 39 android:layout_alignParentStart="true" 40 android:layout_marginTop="30dp" /> 41 42 <EditText 43 android:layout_width="wrap_content" 44 android:layout_height="wrap_content" 45 android:id="@+id/etName" 46 android:inputType="textPersonName" 47 android:ems="10" 48 android:layout_above="@id/tvEmail" 49 android:layout_toEndOf="@id/tvName" 50 android:layout_alignParentRight="true" 51 android:layout_alignParentEnd="true" /> 52 53 54 <EditText 55 android:layout_width="match_parent" 56 android:layout_height="wrap_content" 57 android:id="@+id/etEmail" 58 android:layout_toEndOf="@id/tvEmail" 59 android:inputType="textEmailAddress" 60 android:ems="10" 61 android:layout_above="@id/tvAge" 62 android:layout_alignParentRight="true" 63 android:layout_alignParentEnd="true" /> 64 65 <Button 66 android:layout_width="wrap_content" 67 android:layout_height="wrap_content" 68 android:id="@+id/btnClose" 69 android:text="@string/close" 70 android:layout_alignParentBottom="true" 71 android:layout_alignParentRight="true" 72 /> 73 74 <Button 75 android:layout_width="wrap_content" 76 android:layout_height="wrap_content" 77 android:id="@+id/btnSave" 78 android:text="@string/save" 79 android:layout_alignParentBottom="true" 80 android:layout_toStartOf="@id/btnClose"/> 81 82 <Button 83 android:layout_width="wrap_content" 84 android:layout_height="wrap_content" 85 android:id="@+id/btnDelete" 86 android:text="@string/delete" 87 android:layout_alignParentBottom="true" 88 android:layout_toStartOf="@id/btnSave"/> 89 90 <EditText 91 android:layout_width="match_parent" 92 android:layout_height="wrap_content" 93 android:id="@+id/etAge" 94 android:inputType="number" 95 android:ems="10" 96 android:layout_alignBottom="@+id/tvAge" 97 android:layout_toEndOf="@+id/tvName" /> 98 </RelativeLayout>
4(直接復制自網友,因為我沒有很理解)
當用戶點擊ListView的item時展示學生的詳細信息的activity,所以我們需要一個特殊的id來檢索學生的詳細信息,並且這個id必須來自ListView,可以通過兩個方法實現:
- 最簡單的方法,可以放id和name進listview的item中,展示給用戶(不好的UI設計),當用戶點擊選中item時,將檢索的記錄傳遞到StudentDetail.java的activity。
- 創建一個layout作為listview的item,通過item中包含學生的id(不過設置成隱藏)來檢索學生的詳細信息;
采用第二種方法,創建一個新的layout,view_student_entry.xml,代碼如下:

1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:orientation="vertical" android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:weightSum="1"> 6 7 <TextView 8 android:layout_width="wrap_content" 9 android:layout_height="wrap_content" 10 android:id="@+id/student_Id" 11 android:visibility="gone" 12 android:layout_weight="0.00" /> 13 14 <TextView 15 android:id="@+id/student_Name" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content" 18 android:paddingLeft="6dip" 19 android:paddingTop="6dip" 20 android:textSize="22sp" 21 android:textStyle="bold"/> 22 </LinearLayout>
<二>實現代碼部分:
1,為了創建表,需要使用到SQLiteDatabase類(實現CRUD操作)和SQLiteOpenHelper(用於數據庫的創建和版本管理),
下面先創建一個DBHelper類
CRUD:在MVC系統中,CRUD是什么意思?
CRUD 的意思就是 Create(創建C)、Read(讀取R)、Update(更新U)和Delete(刪除D)的縮寫。
注意:每次我們調用數據庫,都會自動打開並連接,所以注意代碼實現過程中要close()數據庫。

1 public class DBHelper extends SQLiteOpenHelper {
2
3 //version number to upgrade database version
4 //each time if you Add, Edit table, you need to change the
5 //version number.
6 //每次你對數據表進行編輯,添加時候,你都需要對數據庫的版本進行提升
7
8 //數據庫版本
9 private static final int DATABASE_VERSION=3;
10
11 //數據庫名稱
12 private static final String DATABASE_NAME="sqlitestudy.db";
13
14
15 public DBHelper(Context context){
16 super(context,DATABASE_NAME,null,DATABASE_VERSION);
17 }
18
19 @Override
20 public void onCreate(SQLiteDatabase db) {
21 //創建數據表
22 String CREATE_TABLE_STUDENT="CREATE TABLE "+ Student.TABLE+"("
23 +Student.KEY_ID+" INTEGER PRIMARY KEY AUTOINCREMENT ,"
24 +Student.KEY_name+" TEXT, "
25 +Student.KEY_age+" INTEGER, "
26 +Student.KEY_email+" TEXT)";
27 db.execSQL(CREATE_TABLE_STUDENT);
28 }
29
30 @Override
31 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
32 //如果舊表存在,刪除,所以數據將會消失
33 db.execSQL("DROP TABLE IF EXISTS "+ Student.TABLE);
34
35 //再次創建表
36 onCreate(db);
37 }
38 }
2,然后我們進入實體類創建 Student
包含有Student數據庫表的列信息,也可以單獨用於存儲某個學生的信息,作為單個對象進行處理。

1 package examples.ouc.com.sqlitedemo; 2 3 /** 4 * Created by Huskyboy on 2016/10/25. 5 */ 6 7 public class Student { 8 //表名 9 public static final String TABLE="Student"; 10 11 //表的各個域名 12 public static final String KEY_ID="id"; 13 public static final String KEY_name="name"; 14 public static final String KEY_email="email"; 15 public static final String KEY_age="age"; 16 17 //屬性 18 public int student_ID; 19 public String name; 20 public String email; 21 public int age; 22 23 }
3,新建StudentRepo類,編寫CRUD函數。

1 public class StudentRepo { 2 private DBHelper dbHelper; 3 4 public StudentRepo(Context context){ 5 dbHelper=new DBHelper(context); 6 } 7 8 public int insert(Student student){ 9 //打開連接,寫入數據 10 SQLiteDatabase db=dbHelper.getWritableDatabase(); 11 ContentValues values=new ContentValues(); 12 values.put(Student.KEY_age,student.age); 13 values.put(Student.KEY_email,student.email); 14 values.put(Student.KEY_name,student.name); 15 // 16 long student_Id=db.insert(Student.TABLE,null,values); 17 db.close(); 18 return (int)student_Id; 19 } 20 21 public void delete(int student_Id){ 22 SQLiteDatabase db=dbHelper.getWritableDatabase(); 23 db.delete(Student.TABLE,Student.KEY_ID+"=?", new String[]{String.valueOf(student_Id)}); 24 db.close(); 25 } 26 public void update(Student student){ 27 SQLiteDatabase db=dbHelper.getWritableDatabase(); 28 ContentValues values=new ContentValues(); 29 30 values.put(Student.KEY_age,student.age); 31 values.put(Student.KEY_email,student.email); 32 values.put(Student.KEY_name,student.name); 33 34 db.update(Student.TABLE,values,Student.KEY_ID+"=?",new String[] { String.valueOf(student.student_ID) }); 35 db.close(); 36 } 37 38 public ArrayList<HashMap<String, String>> getStudentList(){ 39 SQLiteDatabase db=dbHelper.getReadableDatabase(); 40 String selectQuery="SELECT "+ 41 Student.KEY_ID+","+ 42 Student.KEY_name+","+ 43 Student.KEY_email+","+ 44 Student.KEY_age+" FROM "+Student.TABLE; 45 ArrayList<HashMap<String,String>> studentList=new ArrayList<HashMap<String, String>>(); 46 Cursor cursor=db.rawQuery(selectQuery,null); 47 48 if(cursor.moveToFirst()){ 49 do{ 50 HashMap<String,String> student=new HashMap<String,String>(); 51 student.put("id",cursor.getString(cursor.getColumnIndex(Student.KEY_ID))); 52 student.put("name",cursor.getString(cursor.getColumnIndex(Student.KEY_name))); 53 studentList.add(student); 54 }while(cursor.moveToNext()); 55 } 56 cursor.close(); 57 db.close(); 58 return studentList; 59 } 60 61 public Student getStudentById(int Id){ 62 SQLiteDatabase db=dbHelper.getReadableDatabase(); 63 String selectQuery="SELECT "+ 64 Student.KEY_ID + "," + 65 Student.KEY_name + "," + 66 Student.KEY_email + "," + 67 Student.KEY_age + 68 " FROM " + Student.TABLE 69 + " WHERE " + 70 Student.KEY_ID + "=?"; 71 int iCount=0; 72 Student student=new Student(); 73 Cursor cursor=db.rawQuery(selectQuery,new String[]{String.valueOf(Id)}); 74 if(cursor.moveToFirst()){ 75 do{ 76 student.student_ID =cursor.getInt(cursor.getColumnIndex(Student.KEY_ID)); 77 student.name =cursor.getString(cursor.getColumnIndex(Student.KEY_name)); 78 student.email =cursor.getString(cursor.getColumnIndex(Student.KEY_email)); 79 student.age =cursor.getInt(cursor.getColumnIndex(Student.KEY_age)); 80 }while(cursor.moveToNext()); 81 } 82 cursor.close(); 83 db.close(); 84 return student; 85 } 86 }
4,然后創建StudentDetail的Activity,我們用它來配合顯示每個同學具體信息的界面

1 public class StudentDetail extends AppCompatActivity implements View.OnClickListener { 2 3 Button btnSava, btnDelete; 4 Button btnClose; 5 EditText editTextName; 6 EditText editTextEmail; 7 EditText editTextAge; 8 private int _Student_ID=0; 9 @Override 10 protected void onCreate(Bundle savedInstanceState) { 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_student_detail); 13 14 btnSava = (Button) findViewById(R.id.btnSave); 15 btnDelete = (Button) findViewById(R.id.btnDelete); 16 btnClose = (Button) findViewById(R.id.btnClose); 17 18 btnSava.setOnClickListener(this); 19 btnDelete.setOnClickListener(this); 20 btnClose.setOnClickListener(this); 21 22 _Student_ID = 0; 23 Intent intent = new Intent(); 24 _Student_ID = intent.getIntExtra("student_Id",0); 25 StudentRepo repo = new StudentRepo(this); 26 Student student; 27 student = repo.getStudentById(_Student_ID); 28 29 editTextAge.setText(String.valueOf(student.age)); 30 editTextName.setText(student.name); 31 editTextEmail.setText(student.email); 32 } 33 34 35 @Override 36 public void onClick(View v) { 37 StudentRepo repo = new StudentRepo(this); 38 if (v == findViewById(R.id.btnSave)){ 39 Student student = new Student(); 40 student.age = Integer.parseInt(editTextAge.getText().toString()); 41 student.email = editTextEmail.getText().toString(); 42 student.name = editTextName.getText().toString(); 43 student.student_ID = _Student_ID; 44 45 if(_Student_ID==0){ 46 _Student_ID = repo.insert(student); 47 48 Toast.makeText(this,"New Student Insert",Toast.LENGTH_SHORT).show(); 49 }else{ 50 repo.update(student); 51 Toast.makeText(this,"Student Record updated",Toast.LENGTH_SHORT).show(); 52 } 53 }else if (v == findViewById(R.id.btnDelete)){ 54 repo.delete(_Student_ID); 55 Toast.makeText(this,"Student Record deleted",Toast.LENGTH_SHORT).show(); 56 finish(); 57 }else if (v == findViewById(R.id.btnClose)){ 58 finish(); 59 } 60 } 61 }
5,最后對主界面MainActivity編寫代碼
注意:這里MainActivity一定要技能ListActivity,否則ListView無法使用

1 public class MainActivity extends ListActivity implements android.view.View.OnClickListener { 2 3 private Button btnAdd,btnGetAll; 4 private TextView student_Id; 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 btnAdd = (Button) findViewById(R.id.btnAdd); 10 btnAdd.setOnClickListener(this); 11 12 btnGetAll = (Button) findViewById(R.id.btnGetAll); 13 btnGetAll.setOnClickListener(this); 14 } 15 16 @Override 17 public void onClick(View v) { 18 if (v== findViewById(R.id.btnAdd)){ 19 20 Intent intent = new Intent(MainActivity.this,StudentDetail.class); 21 intent.putExtra("student_Id",0); 22 startActivity(intent); 23 24 }else { 25 26 StudentRepo repo = new StudentRepo(this); 27 ArrayList<HashMap<String, String>> studentList = repo.getStudentList(); 28 if(studentList.size()!=0) { 29 ListView lv = getListView(); 30 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { 31 @Override 32 public void onItemClick(AdapterView<?> parent, View view,int position, long id) { 33 student_Id = (TextView) view.findViewById(R.id.student_Id); 34 String studentId = student_Id.getText().toString(); 35 Intent objIndent = new Intent(getApplicationContext(),StudentDetail.class); 36 objIndent.putExtra("student_Id", Integer.parseInt( studentId)); 37 startActivity(objIndent); 38 } 39 }); 40 ListAdapter adapter = new SimpleAdapter( MainActivity.this,studentList, R.layout.view_student_entry, new String[] { "id","name"}, new int[] {R.id.student_Id, R.id.student_Name}); 41 setListAdapter(adapter); 42 }else{ 43 Toast.makeText(this, "No student!", Toast.LENGTH_SHORT).show(); 44 } 45 46 } 47 } 48 49 }
6,最后附上一點我個人在編寫過程中遇到得一點傻逼錯誤,希望自己和看官老爺(如果有幸的話)引以為戒
我們在學習Intent時候,它可以用來幫忙跳轉界面,也可以用來幫助傳遞數據和參數。
例如下面這個例子(MainActivity中跳轉到添加界面)
if (v== findViewById(R.id.btnAdd)){ Intent intent = new Intent(MainActivity.this,StudentDetail.class); intent.putExtra("student_Id",0); startActivity(intent); }
我們利用intent.putExtra("student_Id",0); 將int型的0,放入了intent,“student_Id”類似於身份的id
在 StudentDetail 我們可以獲取到我們傳入的這個0
1 _Student_ID = 0; 2 Intent intent = new Intent(); 3 //getIntExtra的第二個參數表示: 4 //如果沒有獲取到student_Id的值,那么我們就返回0 5 _Student_ID = intent.getIntExtra("student_Id",0);
而我犯的錯是。。。。開始沒有寫第二個參數,因為getStringExtra是不用寫第二個參數的,返回的應該是null?
再有就是,我前后的 student_Id 居然寫的不一樣,一直獲取不到,導致app 一直各種閃退和崩潰