(一)
知识点:
SQLite3支持的数据类型:NULL,INTEGER,REAL(浮点数字),TEXT(字符串文本),BLOB(二进制对象),虽然他支持的类型只有五种,但实际上sqlite3也接受varchar(n),char(n)等数据类型
1.效果图:点击增加数据,显示增加完毕,点击查询数据,可以查看加进去的数据 。。。。
2.布局
(1)activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.administrator.hello.MainActivity" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="数据库使用" /> <Button android:id="@+id/btn_add" android:onClick="doClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="增加数据"/> <Button android:id="@+id/btn_query" android:onClick="doClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="查询数据"/> <Button android:id="@+id/btn_update" android:onClick="doClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="修改数据"/> <Button android:id="@+id/btn_delete1" android:onClick="doClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除指定数据"/> <Button android:id="@+id/btn_delete2" android:onClick="doClick" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="删除全部数据"/> <ListView android:id="@+id/lv" android:layout_width="match_parent" android:layout_height="wrap_content"></ListView> </LinearLayout>
(2)item.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <TextView 8 android:id="@+id/user_Id" 9 android:layout_width="50dp" 10 android:layout_height="wrap_content" /> 11 <TextView 12 android:id="@+id/user_Name" 13 android:layout_width="100dp" 14 android:layout_height="wrap_content" /> 15 <TextView 16 android:id="@+id/user_Age" 17 android:layout_width="50dp" 18 android:layout_height="wrap_content" /> 19 20 </LinearLayout>
3.UserDao.java
package com.example.administrator.hello.Dao; /** * Created by Administrator on 2018/5/25. */ import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import com.example.administrator.hello.DBUtil.MySQLiteHelper; import com.example.administrator.hello.entity.User; import java.util.ArrayList; import java.util.List; /** * Created by Administrator on 2018/5/24. */ public class UserDao { private MySQLiteHelper mySQLiteHelper; private SQLiteDatabase db; public UserDao(Context context) { mySQLiteHelper = new MySQLiteHelper(context); db = mySQLiteHelper.getReadableDatabase(); //获取数据库对象 } public void insertUser() { //使用字符串拼接方式操作数据 String sql = "insert into t_user(userName,userAge) values ('zhangsan',20)"; db.execSQL(sql); //通过占位符的形式进行数据操作 db.execSQL("insert into t_user(userName,userAge) values(?,?)", new Object[]{"lisi", 21}); //使用系统自带的方法实现数据操作 ContentValues values = new ContentValues(); values.put("userName", "wangwu"); values.put("userAge", 22); db.insert("t_user", null, values); } public List<User> query() { List<User> list = new ArrayList<>(); Cursor cursor = db.rawQuery("select * from t_user", null); if (cursor != null) { while (cursor.moveToNext()) { int userId = cursor.getInt(cursor.getColumnIndex("userId")); String userName = cursor.getString(1); int userAge = cursor.getInt(2); User user = new User(userId, userName, userAge); list.add(user); } } cursor.close(); return list; } public void update(){ db.execSQL("update t_user set userName=? where userId =?",new Object[]{"李四",2}); ContentValues values = new ContentValues(); values.put("userName","王五"); db.update("t_user",values,"userId=?",new String[]{3+""}); } public void update(String name,String id){ db.execSQL("update t_user set userName='"+name+"' where userId="+id); } public void delete(){ //1 db.execSQL("delete from t_user where userId ='1'"); //2 db.execSQL("delete from t_user where userId=?",new Object[]{"2"}); //3 db.delete("t_user","userId=?",new String[]{"3"}); } public void deleteAll(){ db.execSQL("delete from t_user"); } }
4.MySQLiteHelper
package com.example.administrator.hello.DBUtil; /** * Created by Administrator on 2018/5/25. */ import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; /** * Created by Administrator on 2018/5/24. */ public class MySQLiteHelper extends SQLiteOpenHelper { private static final String NAME = "mydata.db"; private static final int version = 1; public MySQLiteHelper(Context context) { super(context, NAME, null, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table t_user(userId integer primary key autoincrement,userName text not null,userAge integer not null)"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
5.User.java
1 package com.example.app5.entity; 2 3 /** 4 * Created by Administrator on 2018/5/24. 5 */ 6 public class User { 7 private int userId; 8 private String userName; 9 private int useAge; 10 11 public User(int userId, String userName, int useAge) { 12 this.userId = userId; 13 this.userName = userName; 14 this.useAge = useAge; 15 } 16 17 public int getUserId() { 18 return userId; 19 } 20 21 public void setUserId(int userId) { 22 this.userId = userId; 23 } 24 25 public String getUserName() { 26 return userName; 27 } 28 29 public void setUserName(String userName) { 30 this.userName = userName; 31 } 32 33 public int getUseAge() { 34 return useAge; 35 } 36 37 public void setUseAge(int useAge) { 38 this.useAge = useAge; 39 } 40 }
6.MainActivity.java
package com.example.administrator.hello; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.ListViewCompat; import android.view.View; import android.widget.ListView; import android.widget.Toast; import com.example.administrator.hello.Dao.UserDao; import com.example.administrator.hello.entity.User; import java.util.List; public class MainActivity extends AppCompatActivity { private UserDao userDao; private List<User> list; private MyAapter myAapter; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); userDao = new UserDao(MainActivity.this); listView = (ListView) findViewById(R.id.lv); } public void doClick(View view) { switch (view.getId()) { case R.id.btn_add: userDao.insertUser(); Toast.makeText(MainActivity.this, "插入数据成功", Toast.LENGTH_SHORT).show(); break; case R.id.btn_query: list = userDao.query(); myAapter = new MyAapter(list, MainActivity.this); listView.setAdapter(myAapter); break; case R.id.btn_update: String name="张三" ; String id ="1"; userDao.update(name,id); userDao.update(); Toast.makeText(MainActivity.this, "修改数据成功", Toast.LENGTH_SHORT).show(); break; case R.id.btn_delete1: userDao.delete(); Toast.makeText(MainActivity.this, "删除指定数据成功", Toast.LENGTH_SHORT).show(); break; case R.id.btn_delete2: userDao.deleteAll(); Toast.makeText(MainActivity.this, "删除全部数据成功", Toast.LENGTH_SHORT).show(); break; } } }
7.MyAapter
1 package com.example.app5; 2 3 import android.content.Context; 4 import android.view.LayoutInflater; 5 import android.view.View; 6 import android.view.ViewGroup; 7 import android.widget.BaseAdapter; 8 import android.widget.TextView; 9 10 import com.example.app5.entity.User; 11 12 import java.util.List; 13 14 /** 15 * Created by Administrator on 2018/5/24. 16 */ 17 public class MyAapter extends BaseAdapter { 18 private Context context; 19 private List<User> list; 20 21 public MyAapter(List<User> list, Context context) { 22 this.list = list; 23 this.context = context; 24 } 25 @Override 26 public int getCount() { 27 return list.size(); 28 } 29 30 @Override 31 public Object getItem(int position) { 32 return list.get(position); 33 } 34 35 @Override 36 public long getItemId(int position) { 37 return position; 38 } 39 40 @Override 41 public View getView(int position, View convertView, ViewGroup parent) { 42 ViewH viewH; 43 if (convertView==null){ 44 convertView= LayoutInflater.from(context).inflate(R.layout.item,null); 45 viewH = new ViewH(); 46 viewH.tv_id=(TextView)convertView.findViewById(R.id.user_Id); 47 viewH.tv_age=(TextView)convertView.findViewById(R.id.user_Age); 48 viewH.tv_name=(TextView)convertView.findViewById(R.id.user_Name); 49 convertView.setTag(viewH); 50 }else { 51 viewH=(ViewH)convertView.getTag(); 52 } 53 viewH.tv_id.setText(list.get(position).getUserId()+""); 54 viewH.tv_name.setText(list.get(position).getUserName()+""); 55 viewH.tv_age.setText(list.get(position).getUseAge()+""); 56 return convertView; 57 } 58 59 class ViewH{ 60 private TextView tv_id; 61 private TextView tv_age; 62 private TextView tv_name; 63 } 64 }