main.xml

<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="插入数据" />
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取数据" />
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="修改数据" /> <Button
android:id="@+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="删除数据" />
</LinearLayout>

main.java

package com.hdjc.sqllitedemo;

import com.hdjc.sqllitedemo.MainActivity;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast; public class MainActivity extends Activity { Button btnAdd;
Button btnQuery;
Button btnUpdate;
Button btnDelete;
SQLiteDatabase db; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 每个应用程序都有独立的Android数据库。不会互相影响。
// 创建一个数据库文件。
db = openOrCreateDatabase("users.db", MODE_PRIVATE, null);
// 创建一张表
db.execSQL("create table if not exists tb_user(id integer primary key autoincrement,name text not null,age integer not null,sex text not null)"); btnAdd = (Button) findViewById(R.id.button1);
btnQuery = (Button) findViewById(R.id.button2);
btnUpdate = (Button) findViewById(R.id.button3);
btnDelete = (Button) findViewById(R.id.button4); btnAdd.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub // 插入数据,使用execsql方式插入数据,缺点在于无法获取返回值类型,插入语句不能省略into
db.execSQL("insert into tb_user(name,sex,age) values('李四','男',20)");
db.execSQL("insert into tb_user(name,sex,age) values('王五','女',28)");
db.execSQL("insert into tb_user(name,sex,age) values('赵六','女',30)"); }
}); btnQuery.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 执行返回游标的查询
Cursor c = db.rawQuery("select * from tb_user", null);
// 循环获取游标中的数据
while (c.moveToNext()) {
// 可以通过c.getXXX获取游标中的数据,参数为int类型,列的索引,可以通过c.getColumnIndex根据名称获取列索引,从0开始
int id = c.getInt(0);
String name = c.getString(c.getColumnIndex("name"));
String sex = c.getString(c.getColumnIndex("sex"));
int age = c.getInt(c.getColumnIndex("age")); Log.i("user", "id:" + id + ",name:" + name + ",sex:" + sex + ",age:" + age);
}
}
}); btnUpdate.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
db.execSQL("update tb_user set name='张三丰' where id=1");
Toast.makeText(MainActivity.this, "修改成功", 1).show();
}
}); btnDelete.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
//删除语句,不能省略from语句
db.execSQL("delete from tb_user where id=1");
Toast.makeText(MainActivity.this, "删除成功", 1).show();
}
});
}
}

Android学习(十) SQLite 基于SQL语句的操作方式的更多相关文章

  1. 七、Android学习第六天——SQLite与文件下载(转)

    (转自:http://wenku.baidu.com/view/af39b3164431b90d6c85c72f.html) 七.Android学习第六天——SQLite与文件下载 SQLite SQ ...

  2. MyBatis学习 之 二、SQL语句映射文件(2)增删改查、参数、缓存

    目录(?)[-] 二SQL语句映射文件2增删改查参数缓存 select insert updatedelete sql parameters 基本类型参数 Java实体类型参数 Map参数 多参数的实 ...

  3. MyBatis学习 之 二、SQL语句映射文件(1)resultMap

    目录(?)[-] 二SQL语句映射文件1resultMap resultMap idresult constructor association联合 使用select实现联合 使用resultMap实 ...

  4. Sqlite常用sql语句

    sqlite常用sql语句 --返回UTC时间 select CURRENT_TIMESTAMP; --返回本地时间 select datetime(CURRENT_TIMESTAMP,'localt ...

  5. Android学习笔记——SQLite

    该工程的功能是实现关于数据库的操作,即creat.update.insert.query.delete 调试的时候请用模拟器,用真机调试的时候进入cmd-adb shell,再进入cd data/da ...

  6. Android学习笔记--Sqlite数据库

    前几天学习了Android中的数据存储,包括文件存储,SharedPreferences存储,还有就是Acndroid中的特色:SQLite数据库存储了.让我比较惊讶的是Android中竟然内嵌了一个 ...

  7. Android学习之SQLite学习

    花了2天时间,系统学习了下Android开发过程中使用的轻量级数据库SQLite的使用. 并掌握其增,删,该,查的基本数据库操作. 首先要使用SQLite数据库,须要通过Android系统提供的SQL ...

  8. (转)Android学习笔记---SQLite介绍,以及使用Sqlite,进行数据库的创建,完成数据添删改查的理解

    原文:http://blog.csdn.net/lidew521/article/details/8655229 1.SQLite介绍:最大特点是,无数据类型;除了可以使用文件或SharedPrefe ...

  9. Android学习总结——SQLite

    SQLiteDatabase类: 一.使用sql语句操作数据库 SQLiteDatabase db = openOrCreateDatabase("database.db", MO ...

随机推荐

  1. hdu 4089 概率dp

    /* 题目大意:注册一款游戏需要排队,一共有四种事件: 1.注册失败,队列不变,概率为p1 2.注册过程中断开连接,正在注册的人排到队列的末尾,概率为p2 3.注册成功,移出队列,概率为p3 4.服务 ...

  2. ABC103

    Wow今天听同学说了这个网站,做了一次比赛的题目,只有四道题. A.三个数a,b,c,找两个最小的差相加,显然是中间数与另外两个数的差,也就是最大值减最小值了 B.两个字符串,判断能否通过对一个进行每 ...

  3. 【原创】Linux环境下的图形系统和AMD R600显卡编程(9)——R600显卡的3D引擎和图形流水线

    1. R600 3D引擎 R600核心是AMD一款非常重要的GPU核心,这个核心引入了统一处理器架构,其寄存器和指令集同以前的GPU 都完全不同,对其编程也有比较大的区别. 图1显示了R600 GPU ...

  4. 27.Remove Element---两指针

    题目链接:https://leetcode.com/problems/remove-element/description/ 题目大意:给出一个数组和一个值,从数组中删除与当前值相等的值,并将数组长度 ...

  5. 12.OpenStack镜像和存储服务配置

    配置镜像服务 编辑 /etc/glance/glance-api.conf与/etc/glance/glance-registry.conf添加以下内容 [DEFAULT] notification_ ...

  6. CDH-5.7.0:基于Parcels方式离线安装配置

    http://shiyanjun.cn/archives/1728.html https://www.waitig.com/cdh%E5%AE%89%E8%A3%85.html

  7. Logstash 最佳实践

    https://doc.yonyoucloud.com/doc/logstash-best-practice-cn/index.html

  8. 【转】玩转Android Camera开发(三):国内首发---使用GLSurfaceView预览Camera 基础拍照demo

    http://blog.csdn.net/yanzi1225627/article/details/33339965 GLSurfaceView是OpenGL中的一个类,也是可以预览Camera的,而 ...

  9. window下Kafka最佳实践

    Kafka的介绍和入门请看这里kafka入门:简介.使用场景.设计原理.主要配置及集群搭建(转) 当前文章从实践的角度为大家规避window下使用的坑. 1.要求: java 6+ 2.下载kafka ...

  10. [thinkphp] MD!! 数组构造的好好的,硬是有一个值无法写入数据库

    我都要抓狂了,buildsql()方法又用不了,最后决定看runtime里面的文件.先删掉所有的runtime,然后提交一次,就可以在runtime里面看到对应解析后的文件,这样应该可以知道问题在哪. ...