activity_main.xml文件:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/LinearLayout1"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:gravity="center_horizontal"
  7. android:orientation="vertical"
  8. tools:context=".MainActivity" >
  9.  
  10. <LinearLayout
  11. android:layout_width="290dp"
  12. android:layout_height="wrap_content"
  13. android:padding="10dp">
  14.  
  15. <TextView
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:text="手机号:"
  19. android:textSize="18sp" />
  20.  
  21. <EditText
  22. android:id="@+id/ID"
  23. android:layout_width="match_parent"
  24. android:layout_height="wrap_content" />
  25. </LinearLayout>
  26.  
  27. <LinearLayout
  28. android:layout_width="290dp"
  29. android:layout_height="wrap_content"
  30. android:padding="10dp">
  31.  
  32. <TextView
  33. android:layout_width="wrap_content"
  34. android:layout_height="wrap_content"
  35. android:text="姓名:"
  36. android:textSize="18dp" />
  37.  
  38. <EditText
  39. android:id="@+id/Name"
  40. android:layout_width="match_parent"
  41. android:layout_height="wrap_content" />
  42. </LinearLayout>
  43.  
  44. <LinearLayout
  45. android:layout_width="wrap_content"
  46. android:layout_height="wrap_content" >
  47. </LinearLayout>
  48.  
  49. <TextView
  50. android:id="@+id/StudentList"
  51. android:layout_width="290dp"
  52. android:layout_height="wrap_content"
  53. android:gravity="center"
  54. android:text="显示数据"
  55. android:padding="10dp"/>
  56.  
  57. </LinearLayout>

main.xml:

  1. <menu xmlns:android="http://schemas.android.com/apk/res/android" >
  2.  
  3. <item
  4. android:id="@+id/CreateDataBase"
  5. android:title="创建数据库">
  6. </item>
  7. <item
  8. android:id="@+id/CreateTable"
  9. android:title="创建表">
  10. </item>
  11. <item
  12. android:id="@+id/DropTable"
  13. android:title="删除表">
  14. </item>
  15. <item
  16. android:id="@+id/InsertData"
  17. android:title="插入数据">
  18. </item>
  19. <item
  20. android:id="@+id/ReadData"
  21. android:title="读取数据">
  22. </item>
  23.  
  24. </menu>

MainActivity.java

  1. package com.zrdm.sqlitecrud;
  2.  
  3. import java.lang.reflect.Field;
  4. import java.util.ArrayList;
  5. import java.util.List;
  6.  
  7. import android.app.Activity;
  8. import android.database.Cursor;
  9. import android.database.sqlite.SQLiteDatabase;
  10. import android.os.Bundle;
  11. import android.view.Menu;
  12. import android.view.MenuItem;
  13. import android.view.View;
  14. import android.view.ViewConfiguration;
  15. import android.widget.EditText;
  16. import android.widget.TextView;
  17. import android.widget.Toast;
  18.  
  19. public class MainActivity extends Activity {
  20.  
  21. // 变量***********************************/
  22. private MyDataBase myDataBase = null;
  23. private String strSql = null;
  24. private EditText ID = null;
  25. private EditText Name = null;
  26. private TextView studentList = null;
  27.  
  28. // 函数***********************************/
  29.  
  30. // 创建
  31. @Override
  32. protected void onCreate(Bundle savedInstanceState) {
  33. super.onCreate(savedInstanceState);
  34. setContentView(R.layout.activity_main);
  35. makeActionOverflowMenuShown();
  36. ID = (EditText) findViewById(R.id.ID);
  37. Name = (EditText) findViewById(R.id.Name);
  38. studentList = (TextView) findViewById(R.id.StudentList);
  39. }
  40.  
  41. // 创建菜单
  42. @Override
  43. public boolean onCreateOptionsMenu(Menu menu) {
  44. // Inflate the menu; this adds items to the action bar if it is present.
  45. getMenuInflater().inflate(R.menu.main, menu);
  46. return true;
  47. }
  48. /*
  49. // 创建表
  50. public void CreateTable(View v) {
  51. strSql = "Create table student(" + "ID nvarchar(20) not null,"
  52. + "Name nvarchar(40) not null," + "primary key(ID)" + " )";
  53. if (myDataBase == null) {
  54. myDataBase = new MyDataBase(this);
  55. }
  56. if (myDataBase.ExecSql(strSql)) {
  57. Toast.makeText(getApplicationContext(), "创建表成功!", Toast.LENGTH_SHORT).show();
  58. } else {
  59. Toast.makeText(getApplicationContext(), "创建表失败!", Toast.LENGTH_SHORT).show();
  60. }
  61.  
  62. }
  63.  
  64. // 删除表
  65. public void DropTable(View v) {
  66. strSql = "Drop table student";
  67. if (myDataBase == null) {
  68. myDataBase = new MyDataBase(this);
  69. }
  70. if (myDataBase.ExecSql(strSql)) {
  71. Toast.makeText(getApplicationContext(), "删除表成功!", Toast.LENGTH_SHORT).show();
  72. } else {
  73. Toast.makeText(getApplicationContext(), "删除表失败!", Toast.LENGTH_SHORT).show();
  74. }
  75.  
  76. }
  77.  
  78. // 插入数据
  79. public void InsertData(View v) {
  80. String strId = ID.getText().toString();
  81. String strName = Name.getText().toString();
  82.  
  83. if (strId.equals("") || strName.equals("")) {
  84. Toast.makeText(getApplicationContext(), "学号或姓名不能为空!", Toast.LENGTH_SHORT).show();
  85. } else {
  86. strSql = "insert into student values (" + "'" + strId + "'," + "'"
  87. + strName + "')";
  88. if (myDataBase == null) {
  89. myDataBase = new MyDataBase(this);
  90. }
  91.  
  92. if (myDataBase.ExecSql(strSql)) {
  93. Toast.makeText(getApplicationContext(), "插入数据成功!", Toast.LENGTH_SHORT).show();
  94. } else {
  95. Toast.makeText(getApplicationContext(), "插入数据失败!", Toast.LENGTH_SHORT).show();
  96. }
  97. }
  98. }
  99.  
  100. // 读取数据
  101. public void ReadData(View v) {
  102. if (myDataBase == null) {
  103. Toast.makeText(getApplicationContext(), "数据库不存在!", Toast.LENGTH_SHORT).show();
  104. } else {
  105. SQLiteDatabase db = myDataBase.getReadableDatabase();
  106. strSql = "select * from student";
  107. Cursor cursor = db.rawQuery(strSql, null);
  108. StringBuffer sb = new StringBuffer();
  109. while (cursor.moveToNext()) {
  110. Student s = new Student(cursor.getString(0),
  111. cursor.getString(1));
  112. sb.append("学号:" + s.getID() + " 姓名:" + s.getName() + "\n");
  113. }
  114. studentList.setText(sb);
  115. }
  116. }(注释区的代码是供按钮使用的因为我把按钮都删掉了所以这一段被我注释掉了,如果想添加按钮的话只需要在布局文件里添加按钮然后在这里吧注释符号去掉去壳)*/
  117.  
  118. // 菜单被选中
  119. @Override
  120. public boolean onOptionsItemSelected(MenuItem item) {
  121. // TODO Auto-generated method stub
  122. int id = item.getItemId();
  123. if (id == R.id.CreateDataBase) {
  124. // 创建数据库
  125. if (myDataBase == null) {
  126. myDataBase = new MyDataBase(this);
  127. Toast.makeText(getApplicationContext(), "创建数据库成功!", Toast.LENGTH_SHORT).show();
  128. } else {
  129. Toast.makeText(getApplicationContext(), "数据库已存在!", Toast.LENGTH_SHORT).show();
  130. }
  131. } else if (id == R.id.CreateTable) {
  132. // 创建表
  133. strSql = "Create table student(" + "ID nvarchar(20) not null,"
  134. + "Name nvarchar(40) not null," + "primary key(ID)" + " )";
  135. if (myDataBase == null) {
  136. myDataBase = new MyDataBase(this);
  137. }
  138. if (myDataBase.ExecSql(strSql)) {
  139. Toast.makeText(getApplicationContext(), "创建表成功!",Toast.LENGTH_SHORT).show();
  140. } else {
  141. Toast.makeText(getApplicationContext(), "创建表失败!", Toast.LENGTH_SHORT).show();
  142. }
  143. } else if (id == R.id.DropTable) {
  144. // 删除表
  145. strSql = "Drop table student";
  146. if (myDataBase == null) {
  147. myDataBase = new MyDataBase(this);
  148. }
  149. if (myDataBase.ExecSql(strSql)) {
  150. Toast.makeText(getApplicationContext(), "删除表成功!", Toast.LENGTH_SHORT).show();
  151. } else {
  152. Toast.makeText(getApplicationContext(), "删除表失败!", Toast.LENGTH_SHORT).show();
  153. }
  154. } else if (id == R.id.InsertData) {
  155. // 插入数据
  156. String strId = ID.getText().toString();
  157. String strName = Name.getText().toString();
  158.  
  159. if (strId.equals("") || strName.equals("")) {
  160. Toast.makeText(getApplicationContext(), "学号或姓名不能为空!", Toast.LENGTH_SHORT).show();
  161. } else {
  162. strSql = "insert into student values (" + "'" + strId + "',"
  163. + "'" + strName + "')";
  164. if (myDataBase == null) {
  165. myDataBase = new MyDataBase(this);
  166. }
  167.  
  168. if (myDataBase.ExecSql(strSql)) {
  169. Toast.makeText(getApplicationContext(), "插入数据成功!", Toast.LENGTH_SHORT)
  170. .show();
  171. } else {
  172. Toast.makeText(getApplicationContext(), "插入数据失败!", Toast.LENGTH_SHORT)
  173. .show();
  174. }
  175. }
  176. } else if (id == R.id.ReadData) {
  177. // 读取数据
  178. if (myDataBase == null) {
  179. Toast.makeText(getApplicationContext(), "数据库不存在!", Toast.LENGTH_SHORT).show();
  180. } else {
  181. SQLiteDatabase db = myDataBase.getReadableDatabase();
  182. strSql = "select * from student";
  183. Cursor cursor = db.rawQuery(strSql, null);
  184. StringBuffer sb = new StringBuffer();
  185. while (cursor.moveToNext()) {
  186. Student s = new Student(cursor.getString(0),
  187. cursor.getString(1));
  188. sb.append( " 姓名:" + s.getName() + "手机号:" + s.getID() +"\n");
  189.  
  190. }
  191. studentList.setText(sb);
  192. }
  193. }
  194.  
  195. return super.onOptionsItemSelected(item);
  196. }
  197.  
  198. // 显示右上角三个点菜单
  199. private void makeActionOverflowMenuShown() {
  200. try {
  201. ViewConfiguration config = ViewConfiguration.get(this);
  202. Field menuKeyField = ViewConfiguration.class
  203. .getDeclaredField("sHasPermanentMenuKey");
  204. if (menuKeyField != null) {
  205. menuKeyField.setAccessible(true);
  206. menuKeyField.setBoolean(config, false);
  207. }
  208. } catch (Exception e) {
  209. // TODO: handle exception
  210. }
  211. }
  212.  
  213. }

MyDataBase.java

  1. package com.zrdm.sqlitecrud;
  2.  
  3. import java.util.concurrent.ExecutionException;
  4.  
  5. import android.content.Context;
  6. import android.database.DatabaseErrorHandler;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10.  
  11. public class MyDataBase extends SQLiteOpenHelper{
  12.  
  13. //变量*********************************************/
  14. private static String strDataBaseName = "Student.db";
  15. private static int intVersion = 1;
  16. private SQLiteDatabase db = null;
  17.  
  18. //函数********************************************/
  19. public MyDataBase(Context context){
  20. super(context, strDataBaseName, null, intVersion);
  21. db = getWritableDatabase();
  22. }
  23. //构造
  24. public MyDataBase(Context context, String name, CursorFactory factory,
  25. int version, DatabaseErrorHandler errorHandler) {
  26. super(context, name, factory, version, errorHandler);
  27. // TODO Auto-generated constructor stub
  28. }
  29.  
  30. //构造
  31. public MyDataBase(Context context, String name, CursorFactory factory,
  32. int version) {
  33. super(context, name, factory, version);
  34. // TODO Auto-generated constructor stub
  35. }
  36.  
  37. //执行SQL语句,建议语句类型 create 、delete 、 update 、insert
  38. public boolean ExecSql(String strSql){
  39. try{
  40. db.execSQL(strSql);
  41. return true;
  42. }catch(Exception e){
  43. return false;
  44. }
  45. }
  46.  
  47. //创建
  48. @Override
  49. public void onCreate(SQLiteDatabase arg0) {
  50. // TODO Auto-generated method stub
  51.  
  52. }
  53.  
  54. //版本升级
  55. @Override
  56. public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  57. // TODO Auto-generated method stub
  58.  
  59. }
  60.  
  61. }

Student.java

  1. package com.zrdm.sqlitecrud;
  2.  
  3. import java.util.concurrent.ExecutionException;
  4.  
  5. import android.content.Context;
  6. import android.database.DatabaseErrorHandler;
  7. import android.database.sqlite.SQLiteDatabase;
  8. import android.database.sqlite.SQLiteDatabase.CursorFactory;
  9. import android.database.sqlite.SQLiteOpenHelper;
  10.  
  11. public class MyDataBase extends SQLiteOpenHelper{
  12.  
  13. //变量*********************************************/
  14. private static String strDataBaseName = "Student.db";
  15. private static int intVersion = 1;
  16. private SQLiteDatabase db = null;
  17.  
  18. //函数********************************************/
  19. public MyDataBase(Context context){
  20. super(context, strDataBaseName, null, intVersion);
  21. db = getWritableDatabase();
  22. }
  23. //构造
  24. public MyDataBase(Context context, String name, CursorFactory factory,
  25. int version, DatabaseErrorHandler errorHandler) {
  26. super(context, name, factory, version, errorHandler);
  27. // TODO Auto-generated constructor stub
  28. }
  29.  
  30. //构造
  31. public MyDataBase(Context context, String name, CursorFactory factory,
  32. int version) {
  33. super(context, name, factory, version);
  34. // TODO Auto-generated constructor stub
  35. }
  36.  
  37. //执行SQL语句,建议语句类型 create 、delete 、 update 、insert
  38. public boolean ExecSql(String strSql){
  39. try{
  40. db.execSQL(strSql);
  41. return true;
  42. }catch(Exception e){
  43. return false;
  44. }
  45. }
  46.  
  47. //创建
  48. @Override
  49. public void onCreate(SQLiteDatabase arg0) {
  50. // TODO Auto-generated method stub
  51.  
  52. }
  53.  
  54. //版本升级
  55. @Override
  56. public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
  57. // TODO Auto-generated method stub
  58.  
  59. }
  60.  
  61. }

点击创建数据库->创建表->插入数据。如果创建表的时候失败就点击删除表然后点击创建表就可以.

插入数据成功:

读取数据成功:

SQLlite实现增删查改的更多相关文章

  1. [置顶] cocos2dx sqllite 增删查改等操作

    首先导入文件shell.c sqllite3.c sqlite3.h sqlite3etx.h文件(注意在生成安卓项目是 不要将shell.c写进android.mk文件中,写进去在cywin中生成会 ...

  2. 6.在MVC中使用泛型仓储模式和依赖注入实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  3. 3.EF 6.0 Code-First实现增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-entity-framework-5-0-code- ...

  4. 4.在MVC中使用仓储模式进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-using-the-repository-pattern-in-mvc/ 系列目录: ...

  5. 5.在MVC中使用泛型仓储模式和工作单元来进行增删查改

    原文链接:http://www.c-sharpcorner.com/UploadFile/3d39b4/crud-operations-using-the-generic-repository-pat ...

  6. jdbc的实例应用:增删查改实现

    //在jdbc中进行增删查改 //查看所有 public static void findAll() { String url = "jdbc:mysql://localhost:3306/ ...

  7. 用javascript实现html元素的增删查改[xyytit]

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  8. hibernate基础增删查改简单实例

    hibernate 基础理论知识网上很多,可以百度和google.这里不做多的介绍,以一个User表来开展例子 建一个web-project 我这里用了junit单元测试环境来进行增删查改的测试,别的 ...

  9. Entity FrameWork 增删查改的本质

    之前的文章里面已经说了,EF的增删查改.那时候的修改,删除,只能是先查询出来要修改的数据,再修改,删除...现在来一个改进版的,增删查改. 1.Add static void Add() { //1. ...

随机推荐

  1. HDFS 01 - HDFS是什么?它的适用场景有哪些?它的架构是什么?

    目录 1.HDFS 是什么 1.1 简单介绍 1.2 发展历史 2.HDFS 应用场景 2.1 适合的应用场景 2.2 不适合的应用场景 3.HDFS 的架构 4.NameNode 和 DataNod ...

  2. 扒几个 3D 模型备用

    前言 在上一篇中,我展示了 OpenGL 开发的基本过程,算是向 3D 世界迈出的一小步吧.对于简单的 3D 物体,比如立方体.球体.圆环等等,我们只需要简单的计算就可以得到他们的顶点的坐标.但是仅仅 ...

  3. 设置mysql的字符集永远为UTF-8

    1.在虚拟机/usr路径下创建一个文件命名为:mysql.cnf cd /usr touch mysql.cnf 2.在该文件中使用vim命令插入配置文本 vim mysql.cnf 按i键进入编辑模 ...

  4. Java中出现Unhandled exception的原因

    说明某个方法在方法声明上已经声明了会抛异常,那么在调用这个方法的时候,就必须做异常处理,处理的方式有2种,要么try-catch这个异常,要么继续往上一层抛出这个异常,这是java语法要求的,必须这么 ...

  5. 解决bs4在python中出现“ImportError: cannot import name ‘HTMLParseError‘”错误

    在使用BeautifulSoup4时候出现了ImportError: cannot import name 'HTMLParseError'的错误. 根本原因是BeautifulSoup在4.4.0以 ...

  6. Spring Boot集成Springfox Swagger3和简单应用

    摘要:Springfox Swagger可以动态生成 API 接口供前后端进行交互和在线调试接口,Spring Boot 框架是目前非常流行的微服务框架,所以,在Spring Boot 项目中集成Sp ...

  7. es6 快速入门 —— 函数

    其他章节请看: es6 快速入门 系列 函数 函数是所有编程语言的重要组成部分,es6之前函数语法一直没什么变化,遗留了许多问题,javaScript开发者多年来不断抱怨,es6终于决定大力度更新函数 ...

  8. Spring Boot 老启动失败,这次再也不怕了!

    Spring Boot 项目是不是经常失败,显示一大堆的错误信息,如端口重复绑定时会打印以下异常: *************************** APPLICATION FAILED TO ...

  9. HDOJ-1074(动态规划+状态压缩)

    Doing Homework HDOJ-1074 1.本题主要用的是状态压缩的方法,将每种状态用二进制压缩表示 2.状态转移方程:dp[i|(1<<j)]=min(dp[i|(1<& ...

  10. 通达OA 任意文件上传-2013/2015版本

    参考 http://wiki.0-sec.org/0day/%E9%80%9A%E8%BE%BEoa/11.html 影响版本 2013版本 2015版本 漏洞文件 general/vmeet/wbU ...