SQLite 这是一个非常流行的嵌入式数据库。它支持 SQL 查询,和只使用很少的内存。Android 在集成实施 SQLite,所以每 Android 应用程序能够使用 SQLite 数据库。对数熟悉 SQL 的开发者来时。使用 SQLite 相当简单。

能够。因为 JDBC 不适合手机这样的内存受限设备。所以 Android 开发者须要学习新的 API 来使用 SQLite。本文以一个注冊登录Demo简介一下sqlite入门使用。

先上一下执行结果:(请忽略丑陋的界面~~)

以下贴上主要代码,后面分析:

/**
* 登录页面的activity
* @author D_xiao
*
*/
public class MainActivity extends Activity {
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button loginbtn = (Button)findViewById(R.id.loginbtn);
Button regbtn = (Button)findViewById(R.id.regbtn);
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null); //创建或打开数据库
loginbtn.setOnClickListener(new OnClickListener(){
public void onClick(View sourse){
boolean flag = false;
String userName = ((EditText)findViewById(R.id.userEditText)).getText().toString();
String userPassword = ((EditText)findViewById(R.id.passwordEditText)).getText().toString();
try{
Cursor cursor = db.rawQuery("select * from users where name = ? and password = ?",new String[]{userName,userPassword}); if(cursor.getCount()==0){
Intent intentE = new Intent(MainActivity.this,ErrorActivity.class);
startActivity(intentE);
}else{
Intent intentS = new Intent(MainActivity.this,HomeActivity.class);
intentS.putExtra("name", userName);
startActivity(intentS);
}
}catch(SQLiteException se){
Intent intentE = new Intent(MainActivity.this,ErrorActivity.class);
startActivity(intentE);
}
}
});
regbtn.setOnClickListener(new OnClickListener(){
public void onClick(View sourse){
Intent intentReg = new Intent(MainActivity.this,RegActivity.class);
startActivity(intentReg);
}
});
}
@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;
}
}
/**
* 注冊Activity
* @author D_xiao
*
*/
public class RegActivity extends Activity {
SQLiteDatabase db;
ListView listView;
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reg);
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null);
final String a = this.getFilesDir().toString();
Button regOK = (Button)findViewById(R.id.regOK);
Button backtologin = (Button)findViewById(R.id.backtologin);
regOK.setOnClickListener(new OnClickListener(){
public void onClick(View sourse){
String name = ((EditText)findViewById(R.id.name)).getText().toString();
String password = ((EditText)findViewById(R.id.password)).getText().toString();
TextView suc = (TextView)findViewById(R.id.suc);
try{
db.execSQL("insert into users values(null,? ,?)",new String[]{name,password});
suc.setText("注冊成功。请点取消button返回到登录界面");
}catch(SQLiteException se){
db.execSQL("create table users(_id integer primary key autoincrement,"
+"name varchar(20) ,"
+"password varchar(200))");
db.execSQL("insert into users values(null,?,?)",new String[]{name,password});
suc.setText("注冊成功。请点取消button返回到登录界面");
}
}
});
backtologin.setOnClickListener(new OnClickListener(){
public void onClick(View sourse){
Intent intent = new Intent(RegActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
public void onDestroy(){
super.onDestroy();
if(db!=null&&db.isOpen()){
db.close();
}
}
@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;
}
}
/**
* 登录成功显示主页,能够发微博 并显示朋友圈
* @author D_xiao
*
*/
public class HomeActivity extends Activity {
SQLiteDatabase db;
ListView listView;
Button btn;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null);
listView = (ListView)findViewById(R.id.show);
btn = (Button)findViewById(R.id.send);
btn.setOnClickListener(new OnClickListener(){
Cursor cursor = null;
public void onClick(View sourse){
String weibo = ((EditText)findViewById(R.id.newtext)).getText().toString();
Intent intent = getIntent();
String name = intent.getStringExtra("name");
try{
insertData(db,name,weibo);
//select * from weibo3
cursor = db.rawQuery("select * from weiboa", null);
inflateList(cursor);
}catch(SQLiteException se){
//primary key autoincrement
db.execSQL("create table weiboa(_id integer primary key autoincrement,"
+"name varchar(20) ,"
+"weibo varchar(200))");
insertData(db,name,weibo);
//查询
cursor = db.rawQuery("select * from weiboa", null);
inflateList(cursor);
}finally{
//cursor.close();
}
}
});
}
private void insertData(SQLiteDatabase db,String name,String weibo){
//运行插入语句
db.execSQL("insert into weiboa values(null,?,?)",new String[]{name,weibo});
}
private void inflateList(Cursor cursor){
//填充SimpleCursorAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(HomeActivity.this,R.layout.line,cursor,
new String[]{"name","weibo"},
new int[]{R.id.my_name,R.id.my_weibo},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
}
public void onDestroy(){
super.onDestroy();
if(db!=null&&db.isOpen()){
db.close();
}
}
@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;
}
}
/**
* username或password错误跳转
* @author D_xiao
*
*/
public class ErrorActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_error); Button backbtn = (Button)findViewById(R.id.Ebackbtn);
backbtn.setOnClickListener(new OnClickListener(){
public void onClick(View sourse){
Intent intentBack = new Intent(ErrorActivity.this,MainActivity.class);
startActivity(intentBack);
}
});
}
@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;
}
}

通过此例,能够看出来sqlite和sql server 等数据库的差别联系:

sqlite没有图形界面,也不须要不论什么的配置安装打开连接等等的操作,几句简单的语句就能够完毕增删改查操作。使用起来还是非常方便的。并且sqlite和sql server ,MySQL是有非常多相似的地方的。除了大多数查询语句在sqlite里面都能够用以外,sqlite还有自己的api提供的方法进行查询,这个以后再叙。并且运行语句也非常相似。

比方db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/android1.db3", null);  这句相当于sql server中的建立连接,所以在使用完以后要关闭连接,都是一样的。

Cursor cursor = db.rawQuery("select * from users where name = ? and password = ?",new String[]{userName,userPassword});这句中的cursor相当于sql server 中的resultSet结思集。

没有图形界面有一点还是比較麻烦的,就是不好操作查看数据表,必需要执行cmd查看,相对来说比較麻烦。请看下篇博文:http://blog.csdn.net/frightingforambition/article/details/24439981

完整Demo:

http://download.csdn.net/detail/u011250851/7248227




版权声明:本文博客原创文章,博客,未经同意,不得转载。

Android学习-----如何使用sqlite对于后台数据交换,sqlite使用例程入门的更多相关文章

  1. Android:Activity+Fragment及它们之间的数据交换.

    Android:Activity+Fragment及它们之间的数据交换 关于Fragment与Fragment.Activity通信的四种方式 比较好一点的Activity+Fragment及它们之间 ...

  2. 【AS3】Flash与后台数据交换四种方法整理

    随着Flash Player 9的普及,AS3编程也越来越多了,所以这次重新整理AS3下几种与后台数据交换方法.1.URLLoader(URLStream)2.FlashRemoting3.XMLSo ...

  3. [转]Android:Activity+Fragment及它们之间的数据交换(一)

    2014-05-18         来源:Android:Activity+Fragment及它们之间的数据交换(一)   简介: 为什么要用Fragment?使用Fragment可以在一个Acti ...

  4. Java核心知识点学习----多线程 倒计时记数器CountDownLatch和数据交换的Exchanger

    本文将要介绍的内容都是Java5中的新特性,一个是倒计时记数器---CountDownLatch,另一个是用于线程间数据交换的Exchanger. 一.CountDownLatch 1.什么是Coun ...

  5. Android:Activity+Fragment及它们之间的数据交换(一)

    简单介绍: 为什么要用Fragment?使用Fragment能够在一个Activity中实现不同的界面. Fragment与Fragment之间的动画切换,远比Activity与Activity之间的 ...

  6. Android学习笔记_37_ListView批量加载数据和页脚设置

    1.在activity_main.xml布局文件中加入ListView控件: <RelativeLayout xmlns:android="http://schemas.android ...

  7. 零基础Android学习笔记-03 窗口间的数据传递

    1.通过全局变量来传递. 新建一个全局的类继承于Application package com.example.helloworld; import android.app.Application; ...

  8. Android学习手记(3) Activity间传递数据

    1. 简单数据传递 建立两个Activity,名称分别为MainActivity和TheAty,在MainActivity中新建一个Button,id为btnStartAty.在TheAty中新建一个 ...

  9. Android学习笔记_8_使用SharedPreferences存储数据

    1.SharedPreferences介绍: Android平台给我们提供了一个SharedPreferences类,它是一个轻量级的存储类,特别适合用于保存软件配置参数.使用SharedPrefer ...

随机推荐

  1. linux路由表配置

    一.原理说明 1.路由表(table)从0到255进行编号,每个编号可以对应一个别名,编号和别名的对应关系在linux下放在/etc/iproute2/rt_tables这个文件里,一般0编号的tab ...

  2. CentOS的配置文件

    /etc/profile:此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/etc/profile.d目录的配置文件中搜集shell的设置. /etc/bashrc:为每一个 ...

  3. 六、Nginx 防盗链

    盗链是指一个网站的资源(图片或附件)未经允许在其它网站提供浏览和下载.尤其热门资源的盗链,对网站带宽的消耗非常大,本文通过nginx的配置指令location来实现简单的图片和其它类型文件的防盗链. ...

  4. JavaScript下全选反选的Demo程序里实现checkmeonly函数 DOM

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 动态规划之一ones

    n给一个整数n,要你找一个值为n的表达式,这个表达式只有1 + * ( ) 够成.并且1不能连续,比如11+1就不合法. n输入n,(1<=n<=10000) n输出最少需要多少个1才能构 ...

  6. 08-IOSCore - App Store、国际化/本地化

    App Store 1. 帐号身份 0. 有Xcode 写程序,在虚拟机上运行 1. 成为苹果使用者 appleid 验证邮箱 权限: 能下载应用程序 2. 成为苹果开发者 https://devel ...

  7. SuperSocket源码解析之消息处理

    一 简述 Tcp消息的处理本身是与Tcp消息传输过程独立的,是消息的两个不同阶段,从前面的会话生命周期我们已经知道消息的传输主要有SocketSession实现,而真正处理则交由AppSession实 ...

  8. Qt线程QThread简析(8个线程等级,在UI线程里可调用thread->wait()等待线程结束,exit()可直接退出线程,setStackSize设置线程堆栈,首次见到Qt::HANDLE,QThreadData和QThreadPrivate)

    QThread实例代表一个线程,我们可以重新实现QThread::run(),要新建一个线程,我们应该先继承QThread并重新实现run()函数. 需要注意的是: 1.必须在创建QThread对象之 ...

  9. c语言统计字符数(判断a-z哪个字符出现次数最多)

    http://poj.grids.cn/practice/2742 描述判断一个由a-z这26个字符组成的字符串中哪个字符出现的次数最多输入第1行是测试数据的组数n,每组测试数据占1行,是一个由a-z ...

  10. 什么是透明(和Windows主题有关系),研究TLable和TPanel是两个好例子

    在controls.pas单元里只有判断,没有赋值,所以一直不是很明白.于是在stdCtrls.pas里找了几个例子,直观加深一下印象: constructor TCustomLabel.Create ...