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. NSDate和NSString

    +(NSDate*) convertDateFromString:(NSString*)uiDate { NSDateFormatter *formatter = [[NSDateFormatter ...

  2. linux中less命令使用

    less与cat和more的区别: cat命令功能用于显示整个文件的内容单独使用没有翻页功能因此经常和more命令搭配使用,cat命令还有就是将数个文件合并成一个文件的功能. more命令功能:让画面 ...

  3. MyEclipse 8.6反编译插件安装

    一.下载插件文件:jad.exe.jadeclipse    http://www.varaneckas.com/sites/default/files/jad/jad158g.win.zip    ...

  4. js 如何验证字符串里是否包含汉字?

    1.用正则表达式判断<input  type="text" id="name" placeholder="请输入用户名" value= ...

  5. Rstudio安装

    1.https://www.r-project.org/下载R语言(注意32位还是46位系统). 2.安装R,尽量默认安装路径,安装路径不要有中文. 3.https://www.rstudio.com ...

  6. 批量更改数据库COLLATION

    企业内部有很多系统是繁体的,由于各方面的原因,公司目前正在实行简体化,但各系统中又有数据间的交换,所以系统只能一个一个的更改,以防同时出现过多的问题.由于原先数据库只能存储繁体,而原先已存在的数据则可 ...

  7. 根据PID和VID得到USB转串口的串口号

    /******************************************************************************* * * FindAppUART.cpp ...

  8. php safe mode bypass all <转>

    PHP safe mode bypass from 4.x to 5.x all. Functions: * mb_send_mail* curl_init* imap_open* mail* ion ...

  9. 关于Java中的程序,进程和线程的详解...

    程序:一段静态的代码,一组指令的有序集合,它本身没有任何运行的含义,它只是一个静态的实体,是应用软件执行的蓝本. 进程:是程序的一次动态执行,它对应着从代码加载,执行至执行完毕的一个完整的过程,是一个 ...

  10. Codeforces Round #336 (Div. 2)B. Hamming Distance Sum 前缀和

    B. Hamming Distance Sum 题目连接: http://www.codeforces.com/contest/608/problem/A Description Genos need ...