ANDROID_MARS学习笔记_S01原始版_009_SQLite
一、代码
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的更多相关文章
- ANDROID_MARS学习笔记_S01原始版_005_RadioGroup\CheckBox\Toast
一.代码 1.xml(1)radio.xml <?xml version="1.0" encoding="utf-8"?> <LinearLa ...
- ANDROID_MARS学习笔记_S01原始版_004_TableLayout
1.xml <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android ...
- ANDROID_MARS学习笔记_S01原始版_003_对话框
1.AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest ...
- ANDROID_MARS学习笔记_S01原始版_002_实现计算乘积及menu应用
一.代码 1.xml(1)activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk ...
- ANDROID_MARS学习笔记_S01原始版_001_Intent
一.Intent简介 二.代码 1.activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.co ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER005_用广播BroacastReciever实现后台播放不更新歌词
一.代码流程1.自定义一个AppConstant.LRC_MESSAGE_ACTION字符串表示广播"更新歌词" 2.在PlayerActivity的onResume()注册Bro ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER004_同步显示歌词
一.流程分析 1.点击播放按钮,会根据lrc名调用LrcProcessor的process()分析歌词文件,得到时间队列和歌词队列 2.new一个hander,把时间队列和歌词队列传给自定义的线程类U ...
- ANDROID_MARS学习笔记_S01原始版_023_MP3PLAYER003_播放mp3
一.简介 1.在onListItemClick中实现点击条目时,跳转到PlayerActivity,mp3info通过Intent传给PlayerActivity 2.PlayerActivity通过 ...
- ANDROID_MARS学习笔记_S01原始版_022_MP3PLAYER002_本地及remote标签
一.简介 1.在main.xml中用TabHost.TabWidget.FrameLayout标签作布局 2.在MainActivity中生成TabHost.TabSpec,调用setIndicato ...
随机推荐
- ASP实现随机提取数据库记录例
<% "一个从数据库中随机读取纪录的例子 Set Rs1=server.CreateObject ("adodb.recordset") Set Rs=server ...
- hive metastore异常 org.apache.thrift.protocol.TProtocolException: Missing version in readMessageBegin, old client
hiveserver2的端口是10000hive.metastoe.uris 的端口9083改为10000之后 beelien 连接hiveserver2报错 Error: Could not ope ...
- 通过公网连接云数据库Memcache--ECS Windows篇
目前云数据库Memcache是需要通过ECS的内网进行连接访问,如果用户本地需要通过公网访问云数据库Memcache,可以在ECS Windows云服务器中通过netsh进行端口映射实现. 一.搭建要 ...
- C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55
//C# 求斐波那契数列的前10个数字 :1 1 2 3 5 8 13 21 34 55 using System; using System.Collections.Generic; using S ...
- ubuntu修改grub背景
grub背景由/etc/grub.d/05_debian_theme定义,修改图片只需要将图片文件放到/boot/grub,d/下即可, 修改颜色只需编辑/boot/grub.d/grub.cfg
- invalid code signing entitlement的通用暴力解决办法
1.添加的一台苹果设备为开发机子后,打版本,说profile 没找到,报错 2.上传二进制文件到itunes connect ,报错 3.有时候还什么 appID 无效,报错 烦死他了 我的解决办法, ...
- 转:jQuery对象与dom对象的转换
jQuery对象与dom对象的转换 发布时间:September 20, 2007 分类:JavaScript <新站上线的手记> <Discuz!多附件上传选择框之jQuery版& ...
- [jquery]高级篇--js绑定事件
参考: http://www.cnblogs.com/leejersey/p/3545372.html jQuery on()方法是官方推荐的绑定事件的一个方法.$(selector).on(eve ...
- [MAXscript Tool]FFX_PalyBack v1.1 ShowReel
自己的写的一个简单的脚本方便实现大面积的烟,火,爆炸,云的效果.能实现静态动态的切换,还有快速的偏移FumeFX的缓存,支持随机缓存 具体看这个插件的ShowReel,结算的三套基础的火焰然后用此脚本 ...
- Java编程思想读书笔记--第21章并发
1.基本的线程机制 定义任务 public class LiftOff implements Runnable{ protected int countDown = 10; private stati ...