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. 使用 padding-bottom 设置高度基于宽度的自适应

    我们在做移动端列表,通常会做到图文列表,列表是自适应的.当列表中有图片,图片的宽度是随着列表宽的变化而变化,我们为了在图片宽度变化的时候做到图片的不变形,所有采用以下办法. 本文章只讲语法 html ...

  2. MySQL-based databases CVE -2016-6663 本地提权

    @date: 2016/11/3 @author: dlive 0x01 漏洞原文 翻译水平不高求轻喷 感觉作者在写文章的时候有些地方描述的也不是特别清楚,不过结合poc可以清晰理解漏洞利用过程 0x ...

  3. CGRectInset、CGRectOffset、等对比整理

    http://blog.sina.com.cn/s/blog_76f3236b01013zmk.html   分类: iphone有关 1.CGRectInsetCGRect CGRectInset ...

  4. 新建module---获取带宽信息

    借鉴自http://blog.csdn.net/xjtuse2014/article/details/53968726 1.MoniterBandwidth模块: package net.floodl ...

  5. centos 搭建ntp

    str=$(printf "%-25s" "*") echo -e "${str// /*}" echo -e "*\t\t\t* ...

  6. Nginx 兼容IE8

    前言 前段时间由于业务需要,在服务器上新增一个服务专门接收各个门店的业务结算数据,接口文档指明需要使用https协议.这本不是什么问题,因为之前服务器已经有配置过https.但等到服务部署之后才发现这 ...

  7. Appium+python自动化6-Remote远程控制【转载】

    前言 在第三篇启动app的时候有这样一行代码driver = webdriver.Remote('http://192.168.1.1:4723/wd/hub', desired_caps),很多小伙 ...

  8. 设计模式原则总结--读《大话设计模式》有感 <转>

    读了<大话设计模式>,摘录该书中讲到的设计模式几大原则,供日后使用. 一.单一职责原则 就一个类而言,应该仅有一个引起它变化的原因.如果一个类承担的职责过多,就等于把这些职责耦合在一起,一 ...

  9. ZOJ 3949 (17th 浙大校赛 B题,树型DP)

    题目链接  The 17th Zhejiang University Programming Contest Problem B 题意  给定一棵树,现在要加一条连接$1$(根结点)和$x$的边,求加 ...

  10. CV3

    Self Assessment: 1.        Skilled in developing with HTML/JavaScript/ASP.NET/C#, experienced in 3-l ...