1.主要核心类,Sqlite编程要继承SQLiteOpenHelper

 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class MySQLiteOpenHelper extends SQLiteOpenHelper {
//创建数据库
public MySQLiteOpenHelper(Context context, String name,
CursorFactory factory, int version) {
//当系统发现version高于在本机上存在的版本时,会执行onUpgrade来更新
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }
}

2.增,查

 package com.example.sqlite;

 import com.example.sqlite.core.MySQLiteOpenHelper;
import com.example.sqlite.listview.SqliteListActivity; import android.support.v7.app.ActionBarActivity;
import android.content.ContentValues;
import android.content.Intent;
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.widget.TextView; public class MainActivity extends ActionBarActivity {
public static final String DB_NAME = "mydb";
public static final String DB_COLUMN_NAME = "name";
public static final String DB_COLUMN_GENDER = "gender";
public static final String DB_TABLE = "user";
MySQLiteOpenHelper dbhelper ; void create_table(){
SQLiteDatabase dbWireter = dbhelper.getWritableDatabase();
String sql = String.format("CREATE TABLE if not exists %s(%s %s DEFAULT %s,%s %s DEFAULT %s)",
DB_TABLE,
DB_COLUMN_NAME,"TEXT","NONE",
DB_COLUMN_GENDER,"TEXT","NONE");
dbWireter.execSQL(sql);
dbWireter.close();
}
void initsqlite(){
dbhelper = new MySQLiteOpenHelper(this,DB_NAME,null,);
create_table();
}
private void write_sqlite(){
SQLiteDatabase dbWireter = dbhelper.getWritableDatabase();
for (int i = ; i < ; i++) {
ContentValues cv = new ContentValues();
cv.put(DB_COLUMN_NAME, "zhang"+i);
cv.put(DB_COLUMN_GENDER, "male");
dbWireter.insert(DB_TABLE, null, cv);
}
TextView out = (TextView) findViewById(R.id.tvOut);
out.setText("write succ");
dbWireter.close();
}
private void select_sqlite(){
SQLiteDatabase dbReader = dbhelper.getReadableDatabase();
Cursor c = dbReader.query(DB_TABLE, null, null, null, null, null, null);
String txt = "";
while(c.moveToNext()){
String name = c.getString(c.getColumnIndex(DB_COLUMN_NAME));
String gender = c.getString(c.getColumnIndex(DB_COLUMN_GENDER));
txt += "name="+name+" gender="+ gender +"\n";
}
TextView out = (TextView) findViewById(R.id.tvOut);
out.setText(txt);
dbReader.close();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initsqlite(); findViewById(R.id.btn_show_inList).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,SqliteListActivity.class));
}
});
} @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;
} @Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch(id){
case R.id.action_create:
create_table();
break;
case R.id.action_write:
write_sqlite();
break;
case R.id.action_read:
select_sqlite();
break;
}
return true;
}
}

3.改,删

 import com.example.sqlite.R;
import com.example.sqlite.core.MySQLiteOpenHelper; import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public class SqliteListActivity extends ActionBarActivity {
static final String DB = "dbinlist";
static final String TABLE = "user";
static final String NAME = "name";
static final String GENDER = "gender"; SimpleCursorAdapter adapter;
ListView list;
Cursor cur;
MySQLiteOpenHelper dbhelper ;
SQLiteDatabase writer,reader; OnItemLongClickListener itemLongListener = new OnItemLongClickListener() { @Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
final int position, long id) {
new AlertDialog.Builder(SqliteListActivity.this).setMessage("what's your choice?")
.setNegativeButton("update",new DialogInterface.OnClickListener() {
//更新
@Override
public void onClick(DialogInterface dialog, int which) {
Cursor c = adapter.getCursor();
c.moveToPosition(position);
int _id = c.getInt(c.getColumnIndex("_id"));
ContentValues cv = new ContentValues();
cv.put(NAME,"未名"+_id );
cv.put(GENDER,"未知"+_id );
writer.update(TABLE, cv,"_id=?",new String[]{_id+""});
refreshView();
}})
.setPositiveButton("delete", new DialogInterface.OnClickListener() {
//删除
@Override
public void onClick(DialogInterface dialog, int which) {
Cursor c = adapter.getCursor();
c.moveToPosition(position);
int _id = c.getInt(c.getColumnIndex("_id"));
writer.delete(TABLE, "_id=?", new String[]{_id+""});//_id=?用问号是防止注入
refreshView();
}
}).show();
return true;
}
}; View.OnClickListener btnListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText name = (EditText) findViewById(R.id.edtName);
final EditText gender = (EditText) findViewById(R.id.edtGender);
ContentValues cv = new ContentValues();
cv.put(NAME, name.getText().toString());
cv.put(GENDER, gender.getText().toString());
writer.insert(TABLE, null, cv);
refreshView();
}
}; void refreshView(){
cur = reader.query(TABLE, null, null, null, null, null, null);
adapter.changeCursor(cur);
}
void createTable(){
String sql = String.format(
"CREATE TABLE if not exists %s(_id INTEGER PRIMARY KEY autoincrement , %s %s DEFAULT %s,%s %s DEFAULT %s)", TABLE,
NAME, "TEXT", "NONE", GENDER, "TEXT", "NONE");
writer.execSQL(sql);
}
void initDataBase(){
dbhelper = new MySQLiteOpenHelper(this,DB,null,);
writer = dbhelper.getWritableDatabase();
reader = dbhelper.getReadableDatabase();
createTable();
refreshView();
}
void initList(){
list = (ListView) findViewById(R.id.sqlite_list);
list.setOnItemLongClickListener(itemLongListener);;
adapter = new SimpleCursorAdapter(this,
R.layout.list_cell, cur,
new String[] { "_id","name","gender" }, new int[] {R.id.tv_id,R.id.tv_cell_name,R.id.tv_cell_gender });
list.setAdapter(adapter);
findViewById(R.id.btnAddUser).setOnClickListener(btnListener);
} @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_sqlite);
initList();
initDataBase();
}
}

Android SQLite(1)简单示例-增,删,改,查的更多相关文章

  1. iOS FMDB的使用(增,删,改,查,sqlite存取图片)

    iOS FMDB的使用(增,删,改,查,sqlite存取图片) 在上一篇博客我对sqlite的基本使用进行了详细介绍... 但是在实际开发中原生使用的频率是很少的... 这篇博客我将会较全面的介绍FM ...

  2. 好用的SQL TVP~~独家赠送[增-删-改-查]的例子

    以前总是追求新东西,发现基础才是最重要的,今年主要的目标是精通SQL查询和SQL性能优化.  本系列主要是针对T-SQL的总结. [T-SQL基础]01.单表查询-几道sql查询题 [T-SQL基础] ...

  3. iOS sqlite3 的基本使用(增 删 改 查)

    iOS sqlite3 的基本使用(增 删 改 查) 这篇博客不会讲述太多sql语言,目的重在实现sqlite3的一些基本操作. 例:增 删 改 查 如果想了解更多的sql语言可以利用强大的互联网. ...

  4. django ajax增 删 改 查

    具于django ajax实现增 删 改 查功能 代码示例: 代码: urls.py from django.conf.urls import url from django.contrib impo ...

  5. ADO.NET 增 删 改 查

    ADO.NET:(数据访问技术)就是将C#和MSSQL连接起来的一个纽带 可以通过ADO.NET将内存中的临时数据写入到数据库中 也可以将数据库中的数据提取到内存中供程序调用 ADO.NET所有数据访 ...

  6. MVC EF 增 删 改 查

    using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...

  7. 简单的php数据库操作类代码(增,删,改,查)

    这几天准备重新学习,梳理一下知识体系,同时按照功能模块划分做一些东西.所以.mysql的操作成为第一个要点.我写了一个简单的mysql操作类,实现数据的简单的增删改查功能. 数据库操纵基本流程为: 1 ...

  8. python基础中的四大天王-增-删-改-查

    列表-list-[] 输入内存储存容器 发生改变通常直接变化,让我们看看下面列子 增---默认在最后添加 #append()--括号中可以是数字,可以是字符串,可以是元祖,可以是集合,可以是字典 #l ...

  9. Android SQLite最简单demo实现(增删查改)

    本来不太想写这篇博客的,但是看到网上的关于android数据库操作的博文都讲得很详细,对于像我这样的新手入门了解SQLite的基本操作有一定难度,所以我参考了网上的一些博客文章,并自己亲自摸索了一遍, ...

随机推荐

  1. StringUtil内部方法差异

    StringUtil 的 isBlank.isEmply.isNotEmpty.isNotBlank 区别   String.trim()方法: trim()是去掉首尾空格   append(Stri ...

  2. ln 软连接 & 硬连接

    创建软连接的方式 #ln -s soure /file object 创建软连接是连接文件本身,可以跨分区建立软连接,不会应为不同分区而出现不能使用的问题. 在创建软连接的文件中,修改一处文件另一处同 ...

  3. [TypeScript] Query Properties with keyof and Lookup Types in TypeScript

    The keyof operator produces a union type of all known, public property names of a given type. You ca ...

  4. poj1161Post Office【经典dp】

    题目:poj1161Post Officeid=1160" target="_blank">点击打开链接 题意:给出一条直线上的n个坐标表示村庄的位置,然后要在上面 ...

  5. coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command

    在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...

  6. HBase 数据迁移

    最近两年负责 HBase,经常被问到一些问题, 本着吸引一些粉丝.普及一点HBase 知识.服务一点阅读人群的目的,就先从 HBase 日常使用写起,后续逐渐深入数据设计.集群规划.性能调优.内核源码 ...

  7. Koa2学习(二)async/await

    Koa2学习(二)async/await koa2中用到了大量的async/await语法,要学习koa2框架,首先要好好理解async/await语法. async/await顾名思义是一个异步等待 ...

  8. 反爬统计 数据库 sql CASE

    -- 经排查日志,发现ordertest.com下的url检测,频繁<Response [403]>,Forbidden;再进一步查询数据库数据:逐日统计错误临时表test_error_t ...

  9. mysql 转换编码方式

    进入mysql 的安装文件夹找到 “ my.ini” 文件  (mysql配置文件) 一.编辑MySql的配置文件 vim /etc/my.cnf 在 [mysqld] 标签下加上三行 default ...

  10. evm指令集手册

    evm指令集手册 Opcodes 结果列为"-"表示没有运算结果(不会在栈上产生值),为"*"是特殊情况,其他都表示运算产生唯一值,并放在栈顶. mem[a.. ...