<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.cunli.sqlite002.MainActivity"> <EditText
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="姓名" /> <EditText
android:id="@+id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/name"
android:hint="年龄" /> <RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/age"> <RadioButton
android:id="@+id/male"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" /> <RadioButton
android:id="@+id/female"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup> <Button
android:id="@+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/radioGroup"
android:onClick="saveStudent"
android:text="插入数据" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/deleteData"
android:onClick="deleteData"
android:layout_marginLeft="20dp"
android:text="删除全部数据"
android:layout_toRightOf="@id/saveButton"
android:layout_below="@id/radioGroup"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/saveButton"
android:hint="更新"
android:id="@+id/updateData"/>
<Button
android:id="@+id/updateButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/updateData"
android:onClick="updateStudent"
android:text="更新"
android:layout_marginLeft="20dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/updateButton"
android:layout_below="@id/updateData"
android:text="取出数据"
android:id="@+id/getData"
android:onClick="getData"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clearData"
android:id="@+id/clearData"
android:layout_below="@id/updateData"
android:layout_toRightOf="@id/getData"
android:text="清空表中所有数据"
android:layout_marginLeft="10dp"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/getData">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="@+id/linerLayout"/> </ScrollView>
</RelativeLayout>
package com.example.cunli.sqlite002;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; import java.util.ArrayList;
import java.util.List; public class MainActivity extends AppCompatActivity {
EditText nameEt,ageEt;
RadioGroup radioGroup;
SQLiteDatabase db;
boolean gender = true;
ContentValues contentValues;
private List<Long> ids = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); contentValues = new ContentValues();
nameEt = (EditText)findViewById(R.id.name);
ageEt = (EditText)findViewById(R.id.age);
radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (checkedId == R.id.male){
gender = true;
}else {
gender = false;
}
}
});
//以刻都写的方式打开或创建一个数据
db = openOrCreateDatabase("school",SQLiteDatabase.OPEN_READWRITE,null);
//建表的语句
String createSQL = "CREATE TABLE IF NOT EXISTS Students (_id INTEGER NOT NULL,student_name CHAR(20) NOT NULL,age INTEGER,gender BOOLEAN,PRIMARY KEY(_id))";
//执行建表SQL语句
db.execSQL(createSQL);
}
//点击按钮事件处理
public void saveStudent(View view){
//创建ContentValues contentValues.put("student_name",nameEt.getText().toString());
contentValues.put("age",Integer.parseInt(ageEt.getText().toString()));
contentValues.put("gender",gender);
//执行插入语句
long id = db.insert("Students",null,contentValues);
ids.add(id);
//如果返回值为-1,则说明那个插入失败
if (id != -1){
Toast.makeText(this,"插入数据成功",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(this,"插入数据 失败",Toast.LENGTH_SHORT).show();
}
} @Override
protected void onDestroy() {
super.onDestroy();
if (db!=null){
db.close();//关闭数据库
}
}
// 更新数据库
public void updateStudent(View view){
String updateData = ((EditText)findViewById(R.id.updateData)).getText().toString();
contentValues.put("student_name",updateData);
//更新数据
int num = db.update("Students",contentValues,"_id=?",new String[]{""+1});
Toast.makeText(MainActivity.this,"更新了"+num+"条数据。",Toast.LENGTH_SHORT).show();
}
// 查询数据
public void getData(View view){
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linerLayout);
linearLayout.removeAllViews();//显示当前数据前,清除之前的数据
Cursor cursor = db.query("Students",new String[]{"student_name","age","gender"},null,null,null,null,"age");
if (cursor.getCount() > 0){
//循环获取数据
while (!cursor.isLast()){//退出循环的条件:是否到cursor的末尾
cursor.moveToNext();//向前移动,注意刚开始的时候,指针是在第一行数据之前的
String name = cursor.getString(0);
int age = cursor.getInt(1);
int gender = cursor.getInt(2);
TextView textView = new TextView(MainActivity.this);
textView.setText(name+":"+age+":"+(gender==1?"男":"女")); linearLayout.addView(textView); }
}else {
Toast.makeText(MainActivity.this,"表中没有数据",Toast.LENGTH_SHORT).show();
} } // 删除全部数据
public void deleteData(View view){
String[] arr = new String[ids.size()];
for (int i = 0; i < ids.size(); i++) {
int delId = db.delete("Students","_id = ? ",new String[]{ids.get(i).toString()});
Log.e("log","----------------"+delId);
}
Toast.makeText(MainActivity.this,"全部数据已经删除",Toast.LENGTH_SHORT).show();
}
// 清空表中所有数据
public void clearData(View view){
int delId = db.delete("Students",null,null);
Log.e("log","----------------"+delId); Toast.makeText(MainActivity.this,"表中全部数据已清空",Toast.LENGTH_SHORT).show();
}
}

sqlite增删改查的更多相关文章

  1. android 入门 006(sqlite增删改查)

    android 入门 006(sqlite增删改查) package cn.rfvip.feb_14_2_sqlite; import android.content.Context; import ...

  2. iOS sqlite 增删改查 简单封装(基于 FMDB)

    /** *  对 sqlite 的使用进行简单封装,仅涉及简单的单表 增删改查 * *  基于 FMDB * *  操作基于 model ,数据库表字段与 model 属性一一对应,对 model 整 ...

  3. C#Sqlite增删改查

    说到使用数据库的话,无非也就是对数据的增加,删除和修改以及查询.前文已经 创建好了程序,现在我们就可以来具体实现Sqlite的数据操作,增删改查. 第一步,创建连接字符串来连接数据库: private ...

  4. iOS SQLite 增删改查的封装(关系型)

    在工程里导入libsqlite3.tbd库(Xcode 7) #import <UIKit/UIKit.h> @interface AppDelegate : UIResponder &l ...

  5. C# 使用 Dapper 实现 SQLite 增删改查

    Dapper 是一款非常不错的轻型 ORM 框架,使用起来非常方便,经常使用 EF 框架的人几乎感觉不到差别,下面是自己写的 Sqlite 通用帮助类: 数据连接类: public class SQL ...

  6. sqlite 增删改查

    PersonDao1.java package mm.shandong.com.testsqlsqllite.dao; import android.content.Context; import a ...

  7. 回家前的挣扎——SQLite增删改查

    引言 最后一天,公司就两个人,也不知道弄点什么,就在网上找了Sqlite的文档,看了看,这里也是现学现卖,给自己找点事做,感觉时间过得还是比较快的,不然焦急等待,滋味不好受啊. SQLite简介 SQ ...

  8. iOS SQLite增删改查(简单应用)

    // 注意: 在工程里导入libsqlite3.tbd库(Xcode7,如果Xcode7以下的版本则导入libsqlite3.dylib). #import <UIKit/UIKit.h> ...

  9. Android Sqlite 增删改查

    模拟 查询所有数据,增加一条数据,修改某一条数据,删除某一条数据: SQLiteOpenHelper 帮助类的介绍: import android.content.Context; import an ...

随机推荐

  1. wp加载本地HTML(附带图片,CSS,JS)

    wp加载本地HTML(附带图片,CSS,JS) Windows Phone:Load Local HTML with Img,Css,Js by 唐小崇 http://www.cnblogs.com/ ...

  2. http概览

    http概览 HTTP协议(HyperText Transfer Protocol,超文本传输协议)是用于从WWW服务器传输超文本到本地浏览器的传送协议.它可以使浏览器更加高效,使网络传输减少.它不仅 ...

  3. Python学习入门基础教程(learning Python)--5.7 Python文件数据记录存储与处理

    本节主要讨论Python下如何通过文件操作实现对数据记录集的存储与处理的操作方法. 在Python里和其他高级语言一样可以通过文件读写将一些记录集写入文件或者通过文件读操作从文件里读取一条或多条和数据 ...

  4. ios的自动转屏

    在IOS6以前,设置转屏需要用到方法 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)x 在6以后,取代它 ...

  5. easyui datebox 时间限制,datebox开始时间限制结束时间,datebox截止日期比起始日期大

    easyui datebox 时间限制,datebox开始时间限制结束时间,datebox截止日期比起始日期大 >>>>>>>>>>> ...

  6. 微信内嵌H5网页 解决js倒计时失效

    项目要求:将H5商城页面嵌套到公司微信公众号里 项目本身的开发跟移动端网页并无太多差异,只是这昨天遇到一个问题,说是棘手,到也简单. 用户下单后,在选择支付方式页面,有个倒计时的逻辑(从下单时开始计算 ...

  7. 你的MongoDB Redis设置用户名密码了吗?看看shodan这款邪恶的搜索引擎吧!~

    早上看新闻的时候看到了个醒目的新闻 开源中国:MongoDB 赎金事件持续发酵,究竟是谁之过? 博客园:MongoDB数据库勒索,中国受害者数量超乎你的想象,SOS! 1. 由于自己之前做过的项目,R ...

  8. RocketMQ源码 — 二、 NameServer

    NameServer 作用:Producer和Consumer获取Broker的地址 目的:解耦Broker和Producer.Consumer 原理:使用netty作为通信工具,监听指定端口,如果是 ...

  9. 用C++实现的元胞自动机

    我是一个C++初学者,控制台实现了一个元胞自动机. 代码如下: //"生命游戏"V1.0 //李国良于2017年1月1日编写完成 #include <iostream> ...

  10. 1 Selenium打开浏览器

    [环境] Selenium3.0.1+Python3.6+unittest win7+IE10 1.打开FireFox浏览器 import unittest from selenium import ...