activity_main.xml文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
tools:context=".MainActivity" > <LinearLayout
android:layout_width="290dp"
android:layout_height="wrap_content"
android:padding="10dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="手机号:"
android:textSize="18sp" /> <EditText
android:id="@+id/ID"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> <LinearLayout
android:layout_width="290dp"
android:layout_height="wrap_content"
android:padding="10dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名:"
android:textSize="18dp" /> <EditText
android:id="@+id/Name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout> <LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</LinearLayout> <TextView
android:id="@+id/StudentList"
android:layout_width="290dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="显示数据"
android:padding="10dp"/> </LinearLayout>

main.xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
android:id="@+id/CreateDataBase"
android:title="创建数据库">
</item>
<item
android:id="@+id/CreateTable"
android:title="创建表">
</item>
<item
android:id="@+id/DropTable"
android:title="删除表">
</item>
<item
android:id="@+id/InsertData"
android:title="插入数据">
</item>
<item
android:id="@+id/ReadData"
android:title="读取数据">
</item> </menu>

MainActivity.java

package com.zrdm.sqlitecrud;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List; import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewConfiguration;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { // 变量***********************************/
private MyDataBase myDataBase = null;
private String strSql = null;
private EditText ID = null;
private EditText Name = null;
private TextView studentList = null; // 函数***********************************/ // 创建
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeActionOverflowMenuShown();
ID = (EditText) findViewById(R.id.ID);
Name = (EditText) findViewById(R.id.Name);
studentList = (TextView) findViewById(R.id.StudentList);
} // 创建菜单
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
/*
// 创建表
public void CreateTable(View v) {
strSql = "Create table student(" + "ID nvarchar(20) not null,"
+ "Name nvarchar(40) not null," + "primary key(ID)" + " )";
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
}
if (myDataBase.ExecSql(strSql)) {
Toast.makeText(getApplicationContext(), "创建表成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "创建表失败!", Toast.LENGTH_SHORT).show();
} } // 删除表
public void DropTable(View v) {
strSql = "Drop table student";
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
}
if (myDataBase.ExecSql(strSql)) {
Toast.makeText(getApplicationContext(), "删除表成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "删除表失败!", Toast.LENGTH_SHORT).show();
} } // 插入数据
public void InsertData(View v) {
String strId = ID.getText().toString();
String strName = Name.getText().toString(); if (strId.equals("") || strName.equals("")) {
Toast.makeText(getApplicationContext(), "学号或姓名不能为空!", Toast.LENGTH_SHORT).show();
} else {
strSql = "insert into student values (" + "'" + strId + "'," + "'"
+ strName + "')";
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
} if (myDataBase.ExecSql(strSql)) {
Toast.makeText(getApplicationContext(), "插入数据成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "插入数据失败!", Toast.LENGTH_SHORT).show();
}
}
} // 读取数据
public void ReadData(View v) {
if (myDataBase == null) {
Toast.makeText(getApplicationContext(), "数据库不存在!", Toast.LENGTH_SHORT).show();
} else {
SQLiteDatabase db = myDataBase.getReadableDatabase();
strSql = "select * from student";
Cursor cursor = db.rawQuery(strSql, null);
StringBuffer sb = new StringBuffer();
while (cursor.moveToNext()) {
Student s = new Student(cursor.getString(0),
cursor.getString(1));
sb.append("学号:" + s.getID() + " 姓名:" + s.getName() + "\n");
}
studentList.setText(sb);
}
}(注释区的代码是供按钮使用的因为我把按钮都删掉了所以这一段被我注释掉了,如果想添加按钮的话只需要在布局文件里添加按钮然后在这里吧注释符号去掉去壳)*/ // 菜单被选中
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
int id = item.getItemId();
if (id == R.id.CreateDataBase) {
// 创建数据库
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
Toast.makeText(getApplicationContext(), "创建数据库成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "数据库已存在!", Toast.LENGTH_SHORT).show();
}
} else if (id == R.id.CreateTable) {
// 创建表
strSql = "Create table student(" + "ID nvarchar(20) not null,"
+ "Name nvarchar(40) not null," + "primary key(ID)" + " )";
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
}
if (myDataBase.ExecSql(strSql)) {
Toast.makeText(getApplicationContext(), "创建表成功!",Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "创建表失败!", Toast.LENGTH_SHORT).show();
}
} else if (id == R.id.DropTable) {
// 删除表
strSql = "Drop table student";
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
}
if (myDataBase.ExecSql(strSql)) {
Toast.makeText(getApplicationContext(), "删除表成功!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "删除表失败!", Toast.LENGTH_SHORT).show();
}
} else if (id == R.id.InsertData) {
// 插入数据
String strId = ID.getText().toString();
String strName = Name.getText().toString(); if (strId.equals("") || strName.equals("")) {
Toast.makeText(getApplicationContext(), "学号或姓名不能为空!", Toast.LENGTH_SHORT).show();
} else {
strSql = "insert into student values (" + "'" + strId + "',"
+ "'" + strName + "')";
if (myDataBase == null) {
myDataBase = new MyDataBase(this);
} if (myDataBase.ExecSql(strSql)) {
Toast.makeText(getApplicationContext(), "插入数据成功!", Toast.LENGTH_SHORT)
.show();
} else {
Toast.makeText(getApplicationContext(), "插入数据失败!", Toast.LENGTH_SHORT)
.show();
}
}
} else if (id == R.id.ReadData) {
// 读取数据
if (myDataBase == null) {
Toast.makeText(getApplicationContext(), "数据库不存在!", Toast.LENGTH_SHORT).show();
} else {
SQLiteDatabase db = myDataBase.getReadableDatabase();
strSql = "select * from student";
Cursor cursor = db.rawQuery(strSql, null);
StringBuffer sb = new StringBuffer();
while (cursor.moveToNext()) {
Student s = new Student(cursor.getString(0),
cursor.getString(1));
sb.append( " 姓名:" + s.getName() + "手机号:" + s.getID() +"\n"); }
studentList.setText(sb);
}
} return super.onOptionsItemSelected(item);
} // 显示右上角三个点菜单
private void makeActionOverflowMenuShown() {
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception e) {
// TODO: handle exception
}
} }

MyDataBase.java

package com.zrdm.sqlitecrud;

import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class MyDataBase extends SQLiteOpenHelper{ //变量*********************************************/
private static String strDataBaseName = "Student.db";
private static int intVersion = 1;
private SQLiteDatabase db = null; //函数********************************************/
public MyDataBase(Context context){
super(context, strDataBaseName, null, intVersion);
db = getWritableDatabase();
}
//构造
public MyDataBase(Context context, String name, CursorFactory factory,
int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
// TODO Auto-generated constructor stub
} //构造
public MyDataBase(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
} //执行SQL语句,建议语句类型 create 、delete 、 update 、insert
public boolean ExecSql(String strSql){
try{
db.execSQL(strSql);
return true;
}catch(Exception e){
return false;
}
} //创建
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub } //版本升级
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub } }

Student.java

package com.zrdm.sqlitecrud;

import java.util.concurrent.ExecutionException;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class MyDataBase extends SQLiteOpenHelper{ //变量*********************************************/
private static String strDataBaseName = "Student.db";
private static int intVersion = 1;
private SQLiteDatabase db = null; //函数********************************************/
public MyDataBase(Context context){
super(context, strDataBaseName, null, intVersion);
db = getWritableDatabase();
}
//构造
public MyDataBase(Context context, String name, CursorFactory factory,
int version, DatabaseErrorHandler errorHandler) {
super(context, name, factory, version, errorHandler);
// TODO Auto-generated constructor stub
} //构造
public MyDataBase(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
} //执行SQL语句,建议语句类型 create 、delete 、 update 、insert
public boolean ExecSql(String strSql){
try{
db.execSQL(strSql);
return true;
}catch(Exception e){
return false;
}
} //创建
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub } //版本升级
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub } }

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

插入数据成功:

读取数据成功:

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. 「NGK每日快讯」12.24日NGK第51期官方快讯!

  2. LoveWord

    个人喜欢的句子汇总! 我告诉你我喜欢你,并不是一定要和你在一起,只是希望今后的你,在遭遇人生低谷的时候,不要灰心,至少曾经有人被你的魅力所吸引,曾经是,以后也会是----村上村树

  3. Dapr 知多少 | 分布式应用运行时

    Intro Dapr 官方团队已于最近(2021.1.17)正式发布Dapr v1.0,Dapr已正式生产可用,可以部署到自托管环境或 Kubernetes 集群.对于绝大多数开发者来说,想必对Dap ...

  4. Angular性能优化实践——巧用第三方组件和懒加载技术

    应该有很多人都抱怨过 Angular 应用的性能问题.其实,在搭建Angular项目时,通过使用打包.懒加载.变化检测策略和缓存技术,再辅助第三方组件,便可有效提升项目性能. 为了帮助开发者深入理解和 ...

  5. C++算法代码——统计数字

    题目来自:http://218.5.5.242:9018/JudgeOnline/problem.php?id=1109 题目描述 某次科研调查时得到了n个自然数,每个数均不超过1500000000( ...

  6. iOS 兼容性处理

    1. scroll滑动层,在iOS中滑动不流畅的处理 -webkit-overflow-scrolling:touch; //在滑动层标签添加这个样式 2. iOS 系统中input标签,去掉圆角效果 ...

  7. hadoop的lzo支持

    目录 1.下载相关文件: 2.Configure LZO to build a shared library (required) and use a package-specific prefix ...

  8. C++数组的存储与初始化

    下面随笔给出C++数组的存储与初始化的细节内容. 数组的存储与初始化 一维数组的存储 数组元素在内存中顺次存放,它们的地址是连续的.元素间物理地址上的相邻,对应着逻辑次序上的相邻. 例如:

  9. 215. 数组中的第K个最大元素 + 快速排序 + 大根堆

    215. 数组中的第K个最大元素 LeetCode-215 另一道类似的第k大元素问题:https://www.cnblogs.com/GarrettWale/p/14386862.html 题目详情 ...

  10. Android - Handler原理

    Handler的主要作用是收发消息和切线程 功能一:收发消息 简单流程介绍 希望你看完这篇文章后也可以把流程自己讲出来,并且每个环节还可以讲出很多细节 他的消息机制离不开Looper.MessageQ ...