sqlite数据库
2016-06-13 11:54
267 查看
分页SQL与mysql类似,下面SQL语句获取5条记录,跳过前面3条记录
select * from Account limit 5 offset 3 或者 select * from Account limit 3,5
插入语句:insert into 表名(字段列表) values(值列表)。如: insert into person(name, age) values(‘传智',3)
更新语句:update 表名 set 字段名=值 where 条件子句。如:update person set name=‘传智‘ where id=10
删除语句:delete from 表名 where 条件子句。如:delete from person where id=10
下面是优化的代码
long starttime = System.currentTimeMillis();
System.out.println(starttime+"");
myDB.beginTransaction();
for (int i = 0; i< 2000;i++){
myDB.execSQL("insert into meetings (id , mainid) values ( '1','1')");
}
myDB.setTransactionSuccessful();
myDB.endTransaction();
long endtime = System.currentTimeMillis()
System.out.println(endtime);
System.out.println((endtime-starttime)/1000);
在执行前开启事务,实行过后再关闭。这样的话,200条数据不到1秒钟,而2000条数据也就才3.7秒钟。
第一步创建帮助类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int VERSON = 1;//默认的数据库版本
//继承SQLiteOpenHelper类的类必须有自己的构造函数
//该构造函数4个参数,直接调用父类的构造函数。其中第一个参数为该类本身;第二个参数为数据库的名字;
//第3个参数是用来设置游标对象的,这里一般设置为null;参数四是数据库的版本号。
public DatabaseHelper(Context context, String name, CursorFactory factory, int verson){
super(context, name, factory, verson);
}
//该构造函数有3个参数,因为它把上面函数的第3个参数固定为null了
public DatabaseHelper(Context context, String name, int verson){
this(context, name, null, verson);
}
//该构造函数只有2个参数,在上面函数 的基础山将版本号固定了
public DatabaseHelper(Context context, String name){
this(context, name, VERSON);
}
//该函数在数据库第一次被建立时调用
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
System.out.println("create a sqlite database");
//execSQL()为执行参数里面的SQL语句,因此参数中的语句需要符合SQL语法,这里是创建一个表
arg0.execSQL("create table user1(id int, name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
System.out.println("update a sqlite database");
}
}
第二部操作数据库
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button create_database = null;
private Button update_database = null;
private Button insert = null;
private Button update = null;
private Button query = null;
private Button delete = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
create_database = (Button)findViewById(R.id.create_database);
crea
4000
te_database.setOnClickListener(new CreateDatabaseOnClickListener());
update_database = (Button)findViewById(R.id.update_database);
update_database.setOnClickListener(new UpdateDatabaseOnClickListener());
insert = (Button)findViewById(R.id.insert);
insert.setOnClickListener(new InsertOnClickListener());
update = (Button)findViewById(R.id.update);
update.setOnClickListener(new UpdateOnClickListener());
query = (Button)findViewById(R.id.query);
query.setOnClickListener(new QueryOnClickListener());
delete = (Button)findViewById(R.id.delete);
delete.setOnClickListener(new DeleteOnClickListener());
}
public class CreateDatabaseOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个DatabaseHelper类的对象,该类是单独一个java文件,这里采用2个参数的构造函数,建立的数据
//库的名字为tornadomeet.db
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
//只有调用getReadableDatabase()或者getWriteableDatabase()函数后才能返回一个SQLiteDatabase对象
SQLiteDatabase db = database_helper.getReadableDatabase();
}
}
public class UpdateDatabaseOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db", 2);
SQLiteDatabase db = database_helper.getReadableDatabase();
}
}
public class InsertOnClickListener implements OnClickListener{
public void onClick(View v) {
// 生成contentvallues对象,该对象用来存数据的
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();//这里是获得可写的数据库
//第一种
db.execSQL("insert into user1(id, name) values(?,?)", new Object[]{1, "xiaoxixo"});
}
}
public class UpdateOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "tornadomeet");
//参数1为表名,参数2为更新后的值,参数3表示满足条件的列名称,参数4为该列名下的值
//第一种,
db.update("user1", values, "id=?", new String[]{"1"});
//第二种,比第一种简单
db.execSQL("update user1 set name='tornado' where id=2");
}
}
public class QueryOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
//查询的语法,参数1为表名;参数2为表中的列名;参数3为要查询的列名;参数时为对应列的值;该函数返回的是一个游标
Cursor cursor = db.query("user1", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null);
//遍历每一个记录
while(cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));//返回列名为name的值
System.out.println("query---->" + name);
}
}
}
public class DeleteOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
//直接删除名为tornadomeet对应的那条记录
db.delete("user1", "name=?" ,new String[]{"tornadomeet"});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局文件
<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" >
<Button
android:id="@+id/create_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/create_database"
/>
<Button
android:id="@+id/update_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/create_database"
android:layout_alignParentLeft="true"
android:text="@string/update_database" />
<Button
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/update_database"
android:layout_alignParentLeft="true"
android:text="@string/insert" />
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/insert"
android:layout_alignParentLeft="true"
android:text="@string/update" />
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@id/update"
android:text="@string/query"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@id/query"
android:text="@string/delete"
/>
</RelativeLayout>
select * from Account limit 5 offset 3 或者 select * from Account limit 3,5
插入语句:insert into 表名(字段列表) values(值列表)。如: insert into person(name, age) values(‘传智',3)
更新语句:update 表名 set 字段名=值 where 条件子句。如:update person set name=‘传智‘ where id=10
删除语句:delete from 表名 where 条件子句。如:delete from person where id=10
下面是优化的代码
long starttime = System.currentTimeMillis();
System.out.println(starttime+"");
myDB.beginTransaction();
for (int i = 0; i< 2000;i++){
myDB.execSQL("insert into meetings (id , mainid) values ( '1','1')");
}
myDB.setTransactionSuccessful();
myDB.endTransaction();
long endtime = System.currentTimeMillis()
System.out.println(endtime);
System.out.println((endtime-starttime)/1000);
在执行前开启事务,实行过后再关闭。这样的话,200条数据不到1秒钟,而2000条数据也就才3.7秒钟。
第一步创建帮助类
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int VERSON = 1;//默认的数据库版本
//继承SQLiteOpenHelper类的类必须有自己的构造函数
//该构造函数4个参数,直接调用父类的构造函数。其中第一个参数为该类本身;第二个参数为数据库的名字;
//第3个参数是用来设置游标对象的,这里一般设置为null;参数四是数据库的版本号。
public DatabaseHelper(Context context, String name, CursorFactory factory, int verson){
super(context, name, factory, verson);
}
//该构造函数有3个参数,因为它把上面函数的第3个参数固定为null了
public DatabaseHelper(Context context, String name, int verson){
this(context, name, null, verson);
}
//该构造函数只有2个参数,在上面函数 的基础山将版本号固定了
public DatabaseHelper(Context context, String name){
this(context, name, VERSON);
}
//该函数在数据库第一次被建立时调用
@Override
public void onCreate(SQLiteDatabase arg0) {
// TODO Auto-generated method stub
System.out.println("create a sqlite database");
//execSQL()为执行参数里面的SQL语句,因此参数中的语句需要符合SQL语法,这里是创建一个表
arg0.execSQL("create table user1(id int, name varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub
System.out.println("update a sqlite database");
}
}
第二部操作数据库
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private Button create_database = null;
private Button update_database = null;
private Button insert = null;
private Button update = null;
private Button query = null;
private Button delete = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
create_database = (Button)findViewById(R.id.create_database);
crea
4000
te_database.setOnClickListener(new CreateDatabaseOnClickListener());
update_database = (Button)findViewById(R.id.update_database);
update_database.setOnClickListener(new UpdateDatabaseOnClickListener());
insert = (Button)findViewById(R.id.insert);
insert.setOnClickListener(new InsertOnClickListener());
update = (Button)findViewById(R.id.update);
update.setOnClickListener(new UpdateOnClickListener());
query = (Button)findViewById(R.id.query);
query.setOnClickListener(new QueryOnClickListener());
delete = (Button)findViewById(R.id.delete);
delete.setOnClickListener(new DeleteOnClickListener());
}
public class CreateDatabaseOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
//创建一个DatabaseHelper类的对象,该类是单独一个java文件,这里采用2个参数的构造函数,建立的数据
//库的名字为tornadomeet.db
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
//只有调用getReadableDatabase()或者getWriteableDatabase()函数后才能返回一个SQLiteDatabase对象
SQLiteDatabase db = database_helper.getReadableDatabase();
}
}
public class UpdateDatabaseOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db", 2);
SQLiteDatabase db = database_helper.getReadableDatabase();
}
}
public class InsertOnClickListener implements OnClickListener{
public void onClick(View v) {
// 生成contentvallues对象,该对象用来存数据的
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();//这里是获得可写的数据库
//第一种
db.execSQL("insert into user1(id, name) values(1, 'abc'), (2, 'hello')");
//第二种
db.execSQL("insert into user1(id, name) values(?,?)", new Object[]{1, "xiaoxixo"});
}
}
public class UpdateOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name", "tornadomeet");
//参数1为表名,参数2为更新后的值,参数3表示满足条件的列名称,参数4为该列名下的值
//第一种,
db.update("user1", values, "id=?", new String[]{"1"});
//第二种,比第一种简单
db.execSQL("update user1 set name='tornado' where id=2");
}
}
public class QueryOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
//查询的语法,参数1为表名;参数2为表中的列名;参数3为要查询的列名;参数时为对应列的值;该函数返回的是一个游标
Cursor cursor = db.query("user1", new String[]{"id", "name"}, "id=?", new String[]{"1"}, null, null, null);
//遍历每一个记录
while(cursor.moveToNext()) {
String name = cursor.getString(cursor.getColumnIndex("name"));//返回列名为name的值
System.out.println("query---->" + name);
}
}
}
public class DeleteOnClickListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
DatabaseHelper database_helper = new DatabaseHelper(MainActivity.this, "tornadomeet.db");
SQLiteDatabase db = database_helper.getWritableDatabase();
//直接删除名为tornadomeet对应的那条记录
db.delete("user1", "name=?" ,new String[]{"tornadomeet"});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
布局文件
<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" >
<Button
android:id="@+id/create_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="@string/create_database"
/>
<Button
android:id="@+id/update_database"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/create_database"
android:layout_alignParentLeft="true"
android:text="@string/update_database" />
<Button
android:id="@+id/insert"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/update_database"
android:layout_alignParentLeft="true"
android:text="@string/insert" />
<Button
android:id="@+id/update"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="@id/insert"
android:layout_alignParentLeft="true"
android:text="@string/update" />
<Button
android:id="@+id/query"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@id/update"
android:text="@string/query"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_above="@id/query"
android:text="@string/delete"
/>
</RelativeLayout>
相关文章推荐
- MySQL安装
- mysql 启动时系统错误1067
- Oracle登录报ORA-01031: insufficient privileges的问题记录
- mysql 字符串类型数字排序
- ( 转)Sqlserver中tinyint, smallint, int, bigint的区别 及 10进制转换16进制的方法
- Memcached+WebApi记录
- sql 嵌套查询
- MySQL 配置
- mybatis-mysql小优化
- mysql常用命令
- MySQL 性能优化的最佳20多条经验分享
- 基于关系型数据库的本体持久化
- PHP7下安装memcached扩展
- Redis从不懂到装懂
- MySQL主从复制与读写分离
- java 实现oracle VPD 权限控制
- mysql为表和字段取别名
- mysql InnoDB 的行锁
- mysql合并查询结果
- oracle 数据泵使用