1.前言:

    今天再一次去蹭了一下某老师的android课,这一次讲的是Android的SQLite的使用,老师当场讲解了他自己做的例子。

  回来之后,我春心萌动,不得不拿着参考资料再做了一个类似的例子,其实我已经过几遍SQLite的内容了,但是认识还是不深刻。

    2.SQLite继承  

    要想使用SQLite,就必需设计一个相应类,并且继承SQLiteOpenHelper。

    基本上要操作的是onCreate函数(注意自动生成,执行语句建议还是单独写),这个函数在数据库被提及时便会执行,所以添加的内容一般就是建表操作。

  person.java

package Model;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class Person extends SQLiteOpenHelper { public static final String CREATE_BOOK = "create table person("
+ "_id integer primary key autoincrement, "
+ "name text, "
+ "tel text)";
public Person(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
// TODO Auto-generated constructor stub
} @Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_BOOK); //建表
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub }
}

  3.使用

    SQLite的使用其实套路都一样的。

personHelper = new Person(this, "personDB.db", null, 1);//new一个类,注意*.db不能跟类重名

    这里因为不理解的原因,“personDB.db”我第一次海用了"person".....

  增加:getWritableDatabase在这里我很不理解,getReadDatabase一般还用不到,看了一次百度的一些解答还是不太懂,那就一直用getWritableDatabase吧。

 SQLiteDatabase db = personHelper.getWritableDatabase();
String str_name = edit_name.getText().toString();
String str_tel = edit_tel.getText().toString();
ContentValues cv = new ContentValues();
cv.put("name", str_name);
cv.put("tel", str_tel);
db.insert("person",null,cv);

  查询:

SQLiteDatabase db = personHelper.getWritableDatabase();
String result = "";
Cursor cursor = db.rawQuery("select * from person", null);
cursor.moveToFirst();
if (cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
String nameString = cursor.getString(1);
String telString = cursor.getString(2);
result += "id="+id+" name:"+nameString+" tel:"+telString+"\n";
Log.d("sql", nameString);
//Log.d("sql", id);
Log.d("sql", telString);
}while(cursor.moveToNext());
}
cursor.close();

  删除:

SQLiteDatabase db = personHelper.getWritableDatabase();
String str_name = edit_name.getText().toString();
String str_tel = edit_tel.getText().toString();
db.delete("person", "name=? and tel=?", new String[]{str_name,str_tel});

    修改:

SQLiteDatabase db = personHelper.getWritableDatabase();
String str_name = edit_name.getText().toString();
String str_tel = edit_tel.getText().toString();
ContentValues cv = new ContentValues();
cv.put("tel", str_tel);
db.update("person", cv, "name=?", new String[]{str_name});

  Mainactivity.java

package com.sqlitetest.app;

import Model.Person;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; public class MainActivity extends Activity { Button btn_add = null;
Button btn_delete = null;
Button btn_update = null;
Button btn_search = null;
EditText edit_name = null;
EditText edit_tel = null;
TextView txt_result = null;
Person personHelper = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
personHelper = new Person(this, "personDB.db", null, 1);//new一个类,注意*.db不能跟类重名
btn_add = (Button) findViewById(R.id.btn_add);
btn_delete = (Button) findViewById(R.id.btn_delete);
btn_search = (Button) findViewById(R.id.btn_search);
btn_update = (Button) findViewById(R.id.btn_update);
edit_name = (EditText) findViewById(R.id.edit_name);
edit_tel = (EditText) findViewById(R.id.edit_number);
txt_result = (TextView) findViewById(R.id.txt_result);
txt_result.setMovementMethod(new ScrollingMovementMethod()); //使得内容多时textview可以滚动 btn_add.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db = personHelper.getWritableDatabase();
String str_name = edit_name.getText().toString();
String str_tel = edit_tel.getText().toString();
ContentValues cv = new ContentValues();
cv.put("name", str_name);
cv.put("tel", str_tel);
db.insert("person",null,cv);
Log.d("sql", str_name);
Log.d("sql", str_tel);
}
}); btn_search.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db = personHelper.getWritableDatabase();
String result = "";
Cursor cursor = db.rawQuery("select * from person", null);
cursor.moveToFirst();
if (cursor.moveToFirst()){
do{
int id = cursor.getInt(0);
String nameString = cursor.getString(1);
String telString = cursor.getString(2);
result += "id="+id+" name:"+nameString+" tel:"+telString+"\n";
Log.d("sql", nameString);
//Log.d("sql", id);
Log.d("sql", telString);
}while(cursor.moveToNext());
}
cursor.close();
txt_result.setText(result);
}
}); btn_delete.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db = personHelper.getWritableDatabase();
String str_name = edit_name.getText().toString();
String str_tel = edit_tel.getText().toString();
db.delete("person", "name=? and tel=?", new String[]{str_name,str_tel});
}
});
btn_update.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
SQLiteDatabase db = personHelper.getWritableDatabase();
String str_name = edit_name.getText().toString();
String str_tel = edit_tel.getText().toString();
ContentValues cv = new ContentValues();
cv.put("tel", str_tel);
db.update("person", cv, "name=?", new String[]{str_name});
}
});
}
}

activity_main.xml

<RelativeLayout 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"
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.sqlitetest.app.MainActivity" > <TextView
android:id="@+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="20dp"
android:layout_marginTop="20dp"
android:text="@string/name"
android:textSize="20sp" /> <EditText
android:id="@+id/edit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txt_name"
android:layout_alignBottom="@+id/txt_name"
android:layout_marginLeft="21dp"
android:layout_toRightOf="@+id/txt_name"
android:ems="10"
android:inputType="textPersonName" /> <EditText
android:id="@+id/edit_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/txt_number"
android:layout_alignLeft="@+id/edit_name"
android:ems="10"
android:inputType="number" /> <TextView
android:id="@+id/txt_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/edit_name"
android:layout_marginTop="18dp"
android:layout_toLeftOf="@+id/edit_name"
android:text="@string/phonenumber"
android:textSize="20sp" /> <ScrollView
android:id="@+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/btn_update"
android:layout_below="@+id/btn_update" >
</ScrollView> <Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/edit_number"
android:layout_below="@+id/edit_number"
android:layout_marginRight="47dp"
android:text="@string/add" /> <Button
android:id="@+id/btn_delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/btn_update"
android:layout_toRightOf="@+id/txt_number"
android:text="@string/delete" /> <TextView
android:id="@+id/txt_result"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignTop="@+id/scrollView1"
android:layout_toRightOf="@+id/txt_number"
android:maxLines = "1000"//这个滚动要加上去,最大怎么表示不清楚
android:scrollbars = "vertical"//这个也是滚动必须加的
android:text="@string/result" /> <Button
android:id="@+id/btn_update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btn_add"
android:layout_below="@+id/btn_add"
android:layout_marginTop="16dp"
android:text="@string/update" /> <Button
android:id="@+id/btn_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/txt_result"
android:layout_alignLeft="@+id/txt_result"
android:text="@string/search" /> </RelativeLayout>

    

Android SQLite 的简单实例的更多相关文章

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

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

  2. Android SQLite(1)简单示例-增,删,改,查

    1.主要核心类,Sqlite编程要继承SQLiteOpenHelper import android.content.Context; import android.database.sqlite.S ...

  3. Android之SimpleAdapter简单实例和SimpleAdapter参数说明

    SimpleAdapter基本上认知了其参数含义 用起来就简单多了 SimpleAdapter的参数说明 第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要  第二个参数表示生 ...

  4. android json 解析简单实例

    Android JSON解析跟JAVA 的JSON解析原理是一样的. Android自带的JSON方式跟方便,不需要导包啥的.不深究原理了,直接上代码: public class JsonActivi ...

  5. Android属性动画-简单实例

    1.ValueAnimator //在2000毫秒内,将值从0过渡到1的动画 ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f); anim.setD ...

  6. Android——SQLite/数据库 相关知识总结贴

    android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...

  7. [Android]RecyclerView的简单演示样例

    去年google的IO上就展示了一个新的ListView.它就是RecyclerView. 下面是官方的说明,我英语能力有限,只是我大概这么理解:RecyclerView会比ListView更具有拓展 ...

  8. 【转】Android Https服务器端和客户端简单实例

    转载地址:http://blog.csdn.net/gf771115/article/details/7827233 AndroidHttps服务器端和客户端简单实例 工具介绍 Eclipse3.7 ...

  9. Android Fragment 简单实例

    Android上的界面展示都是通过Activity实现的.Activity实在是太经常使用了.我相信大家都已经很熟悉了,这里就不再赘述. 可是Activity也有它的局限性,相同的界面在手机上显示可能 ...

随机推荐

  1. 深入学习JavaScript对象

    JavaScript中,除了五种原始类型(即数字,字符串,布尔值,null,undefined)之外的都是对象了,所以,不把对象学明白怎么继续往下学习呢? 一.概述 对象是一种复合值,它将很多值(原始 ...

  2. [置顶] 我的设计模式学习笔记------>Java设计模式总概况

    设计模式的概念最早起源于建筑设计大师Alexander的<建筑的永恒方法>一书,尽管Alexander的著作是针对建筑领域的,但是他的观点实际上用用于所有的工程设计领域,其中也包括软件设计 ...

  3. POJ 3170 Knights of Ni (暴力,双向BFS)

    题意:一个人要从2先走到4再走到3,计算最少路径. 析:其实这个题很水的,就是要注意,在没有到4之前是不能经过3的,一点要注意.其他的就比较简单了,就是一个双向BFS,先从2搜到4,再从3到搜到4, ...

  4. Spring JTA应用JOTM & Atomikos III Atomikos

    前面简单介绍了JOTM如何在Spring中配置,并如何使用它的JTA事务,本节将介绍Atomikos如何与Spring集成,并使用它的JTA事务. Atomikos,是一个基于Java的开源事务管理器 ...

  5. The_Last_Geass

    我在此立下最终的Flag,为了让它保持在第一条我不会再发任何说说:从吃晚饭开始心情就有些崩溃,感觉毫无希望.一直到现在三个多小时吧,想了很多写了很多也跑了两圈步,也许明白了些什么. 现在1月,距离省选 ...

  6. c#学习之旅------01

    一.交换两个数的值 //交换两个数的值 #region 方法一 , num2 = ;//待交换的两个数值 int temp;//临时变量 temp = num1; num1 = num2; num2 ...

  7. ab性能并发测试语法

    ab测试语法ab -n 全部请求数 -c 并发数 测试url 例如:ab -n 10000 -c 1000 http://myweb.com/test.html Server Software: Ap ...

  8. 浅谈Oracle函数返回Table集合

    在调用Oracle函数时为了让PL/SQL 函数返回数据的多个行,必须通过返回一个 REF CURSOR 或一个数据集合来完成.REF CURSOR 的这种情况局限于可以从查询中选择的数据,而整个集合 ...

  9. 剖析SSH核心原理(一)

      在我前面的文章中,也试图总结过SSH,见 http://blog.csdn.net/shan9liang/article/details/8803989 ,随着知识的积累,总感觉以前说得比较笼统, ...

  10. .NET开发中的事务处理大比拼

    本文转载:http://www.cnblogs.com/jhxk/articles/2696307.html http://liubaolongg.blog.163.com/blog/static/2 ...