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. EIGRP默认路由分发的四种方法

    方式一:重发布静态 R2(config)#ip route 0.0.0.0 0.0.0.0 s1/1 R2(config)#router eig 10 R2(config-router)#redist ...

  2. PPPOE拨号演练

    搭建PPPOE服务器,拓扑如下 PPP-Server配置如下: (config)#bba-group pppoe global (config-bba-group)#virtual-template ...

  3. HDU 1312 Red and Black DFS(深度优先搜索) 和 BFS(广度优先搜索)

    Red and Black Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  4. 队列与DelphiXe新语法

    好久没写代码了,更久没上博客园的博客了,无聊写几行试一下新语法. 1 unit Main; interface uses Winapi.Windows, Winapi.Messages, System ...

  5. JavaServer Faces 2.2 requires Dynamic Web Module 2.5 or newer

    Description Resource Path Location Type JavaServer Faces 2.2 can not be installed : One or more cons ...

  6. 利用HTML5开发Android(4)---HTML5本地存储之Web Storage

    Web Storage是HTML5引入的一个非常重要的功能,可以在客户端本地存储数据,类似HTML4的cookie,但可实现功能要比cookie强大的多,cookie大小被限制在4KB,Web Sto ...

  7. maven中如何打包源代码

    http://yanghaoyuan.iteye.com/blog/2032406 使用Maven对项目部署太方便了,特别是依赖关系,最近学习使用Maven,为了备忘和技术的分享特意注册个账号记录到博 ...

  8. 编译cscope-15.8a遇到的问题与解决方案

    1)环境 主机:Linux ubuntu 3.2.0-32-generic-pae #51-Ubuntu SMP Wed Sep 26 21:54:23 UTC 2012 i686 i686 i386 ...

  9. Hadoop on Mac with IntelliJ IDEA - 5 解决java heap space问题

    本文讲述在CentOS 6.5中提交作业到hadoop 1.2.1于reduce阶段遇到Error: java heap space错误导致作业重新计算的解决过程.解决办法适用Linux.Mac OS ...

  10. 在ASP.NET中支持断点续传下载大文件(ZT)

    IE的自带下载功能中没有断点续传功能,要实现断点续传功能,需要用到HTTP协议中鲜为人知的几个响应头和请求头. 一. 两个必要响应头Accept-Ranges.ETag         客户端每次提交 ...