一、代码
1.xml
(1)activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/createDatabase"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="createDatabase"
/> <Button
android:id="@+id/updateDatabase"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="updateDatabase"
/> <Button
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="insert"/>
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="update"/>
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="query"/> </LinearLayout>

2.java
(1)MainActivity.java

 package com.example.s01_original_e16_sqlite;

 import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity { /** Called when the activity is first created. */
private Button createButton;
private Button insertButton;
private Button updateButton;
private Button updateRecordButton;
private Button queryButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
createButton = (Button)findViewById(R.id.createDatabase);
updateButton = (Button)findViewById(R.id.updateDatabase);
insertButton = (Button)findViewById(R.id.insert);
updateRecordButton = (Button)findViewById(R.id.update);
queryButton = (Button)findViewById(R.id.query);
createButton.setOnClickListener(new CreateListener());
updateButton.setOnClickListener(new UpdateListener());
insertButton.setOnClickListener(new InsertListener());
updateRecordButton.setOnClickListener(new UpdateRecordListener());
queryButton.setOnClickListener(new QueryListener());
}
class CreateListener implements OnClickListener{
@Override
public void onClick(View v) {
//创建一个DatabaseHelper对象
DBHelper dbHelper = new DBHelper(MainActivity.this, "my_sqlite_db");
//只有调用了DatabaseHelper对象的getReadableDatabase()方法,或者是getWritableDatabase()方法之后,才会创建,或打开一个数据库
SQLiteDatabase db = dbHelper.getReadableDatabase();
}
}
class UpdateListener implements OnClickListener{ @Override
public void onClick(View v) {
DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2);
SQLiteDatabase db = dbHelper.getReadableDatabase();
} }
class InsertListener implements OnClickListener{ @Override
public void onClick(View v) {
//生成ContentValues对象
ContentValues values = new ContentValues();
//想该对象当中插入键值对,其中键是列名,值是希望插入到这一列的值,值必须和数据库当中的数据类型一致
values.put("id", 1);
values.put("name","zhangsan");
DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2);
SQLiteDatabase db = dbHelper.getWritableDatabase();
//调用insert方法,就可以将数据插入到数据库当中
db.insert("user", null, values);
}
}
//更新操作就相当于执行SQL语句当中的update语句
//UPDATE table_name SET XXCOL=XXX WHERE XXCOL=XX...
class UpdateRecordListener implements OnClickListener{ @Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//得到一个可写的SQLiteDatabase对象
DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "zhangsanfeng");
//第一个参数是要更新的表名
//第二个参数是一个ContentValeus对象
//第三个参数是where子句
db.update("user", values, "id=?", new String[]{"1"});
}
}
class QueryListener implements OnClickListener{ @Override
public void onClick(View v) {
System.out.println("aaa------------------");
Log.d("myDebug", "myFirstDebugMsg"); DBHelper dbHelper = new DBHelper(MainActivity.this,"my_sqlite_db",2);
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.query("user", new String[]{"id","name"}, "id=?", new String[]{"1"}, null, null, null);
while(cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex("name"));
System.out.println("query--->" + name);
}
}
} }

(2)DBHelper.java

 package com.example.s01_original_e16_sqlite;

 import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper; public class DBHelper extends SQLiteOpenHelper { private static final int VERSION = 1;
//在SQLiteOepnHelper的子类当中,必须有该构造函数
public DBHelper(Context context, String name, CursorFactory factory,
int version) {
//必须通过super调用父类当中的构造函数
super(context, name, factory, version);
// TODO Auto-generated constructor stub
}
public DBHelper(Context context,String name){
this(context,name,VERSION);
}
public DBHelper(Context context,String name,int version){
this(context, name,null,version);
} //该函数是在第一次创建数据库的时候执行,实际上是在第一次得到SQLiteDatabse对象的时候,才会调用这个方法
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
System.out.println("create a Database");
//execSQL函数用于执行SQL语句
db.execSQL("create table user(id int,name varchar(20))");
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
System.out.println("update a Database");
} }

ANDROID_MARS学习笔记_S01原始版_009_SQLite的更多相关文章

  1. ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast

    一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...

  2. ANDROID_MARS学习笔记_S01原始版_004_TableLayout

    1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...

  3. ANDROID_MARS学习笔记_S01原始版_003_对话框

    1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...

  4. ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用

    一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...

  5. ANDROID_MARS学习笔记_S01原始版_001_Intent

    一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...

  6. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词

    一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...

  7. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词

    一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...

  8. ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3

    一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...

  9. ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签

    一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...

随机推荐

  1. poj2594 (最小路径覆盖 + floyd)

    题目链接  http://poj.org/problem?id=2594) 题目大意: 一个有向图中, 有若干条连接的路线, 问最少放多少个机器人,可以将整个图上的点都走过. 最小路径覆盖问题. 分析 ...

  2. HTTP和HTTPS详解

    http://blog.csdn.net/mingli198611/article/details/8055261/ 转自:http://www.cnblogs.com/ok-lanyan/archi ...

  3. Merge Into example

    merge into users a using temp_users b on (a.userid = b.user_id) when matched then update set a.passw ...

  4. touches,motion触摸事件响应

    //触摸事件响应需要重写方法 1 // 触摸时触发该方法(消息发送) - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent: ...

  5. table表格中加入<a>标签,使内容上下居中的方法。

    主要思路:定义好表格单元格的width和height,再加入<a>后,设置<a>的width=100%,height=100%填充整个单元格.那么此时设置上下左右居中样式就只需 ...

  6. 国内IT技术博客对比

    今天我想就自己对用了国内几个IT行业领先的博客做一个心得体会的总结: 我总共是用了三个,第一个是新浪,第二个是CSDN,第三个是博客园: 当然期间有自己搭建过wordpress,也用了一段时间,但是感 ...

  7. Linux网络应用编程之VLAN(Packet Tracer仿真)

    VLAN 一.VLAN概述 VLAN(虚拟局域网):将多个设备和用户在逻辑上联网在一起,这些设备和用户不受物理位置的限制(物理设备可以在不同的地方),但是他们的通信就好像在同一网段中一样,这就叫VLA ...

  8. HaProxy+keepalived实现负载均衡

    HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.HAProxy特别适用于那些负载特大的web站点,这些站点通常又需要会话保持 ...

  9. python计算两个日期时间差

    经常在python中因为日期而google,在此做个小笔记. >>>import datetime >>> a = datetime.date.today() &g ...

  10. 没用调用flush导致的数据保存丢失

    在将字符串保存到文件时,我们采有下面的写法,大部分情况下,都可以直接将数据保存到文件中, using (var fs = System.IO.File.Create(path)) { var sw = ...