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. rand和srand的用法(转载)

    首先我们要对rand&srand有个总体的看法:srand初始化随机种子,rand产生随机数,下面将详细说明. rand(产生随机数)表头文件: #include<stdlib.h> ...

  2. Coding Ninja 修炼笔记 (1)

    大家好啊~我又回来了. 这次主要是给大家带来一些提升 Coding 效率的建议. 效率都是一点一滴优化出来的,虽然每一条建议给你带来的提升可能都不大,但是积累起来,仍然是一股不可忽视的力量. 第一条 ...

  3. mongodb数据出现undefined如何查询

    某些字段出现undefined,该如何查询? 如下: db.getCollection('license').find({"holder_code":{$type:"un ...

  4. Java反射的基本应用

    反射机制,程序在运行时加载新的类,使程序更加灵活 public class HelooReflect { public static void main(String[] args) { // 获取类 ...

  5. jquery中注意点

    1.jquery.fn.extend和jquery.extend jquery.fn.extend是向jquery中的prototype中添加方法或者属性,而jquery.extend是向jquery ...

  6. POJ1984 Navigation Nightmare —— 种类并查集

    题目链接:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K T ...

  7. [Django基础] django解决静态文件依赖问题以及前端引入方式

    一.静态文件依赖 学习django的时候发现静态文件(css,js等)不能只在html中引入,还要在项目的settings中设置,否则会报以下错误 [11/Sep/2018 03:18:15] &qu ...

  8. bzoj 1898

    1898: [Zjoi2005]Swamp 沼泽鳄鱼 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 1197  Solved: 661[Submit][S ...

  9. UI:SQL语句

    sql语句一般不区分大小写,但是我们默认的是关键字要大写是一种好的习惯,比如SELECT 等效于 select.,但是表中的字段,属性区分大小写.Oracle 数据库是一种区分大小写的. Sql语句命 ...

  10. EF include 预先加载

    在asp.net mvc 中,常在控制器中预先加载导航属性,以便在视图中能够显示起关联的数据. 如果不预先加载,View中就会无法呈现外键的 关联数据. 会提示EF 错误发生. 一. 模型: publ ...