使用SQLiteHelper创建数据库并插入数据
参考《疯狂android讲义》8.4节P424
1、获取SQLiteDatabase实例有2种方法,一是直接new SQLiteDatabase(),另一种使用SQLiteHelper。一般建议使用后者。
使用SQLiteHelper插入数据的一般步骤:
package com.ljh.sqllitehelperdemo.helper; 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 { final private String CREATE_TABLE_SQL = "create table dict(_id integer primary key autoincrement, word, detail)"; public DatabaseHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
} //1、创建SQLiteOpenHelper的子类,并重写onCreate及onUpgrade方法。
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_SQL);
} @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.ljh.sqllitehelperdemo; import com.ljh.sqllitehelperdemo.helper.DatabaseHelper; import android.os.Bundle;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast; public class MainActivity extends Activity { private EditText etWord = null;
private EditText etDetail = null;
private EditText etSearchWord= null;
private TextView tvDetail = null;
private Button btInsert = null;
private Button btSearch = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); etWord = (EditText) findViewById(R.id.et_word);
etDetail = (EditText) findViewById(R.id.et_detail);
etSearchWord = (EditText) findViewById(R.id.et_search_content);
tvDetail = (TextView) findViewById(R.id.tv_detail);
btInsert = (Button) findViewById(R.id.bt_insert);
btSearch = (Button) findViewById(R.id.bt_search); //2、获取SQLiteOpenHelper的实例,并由此获取SQLiteDatabase实例。
DatabaseHelper helper = new DatabaseHelper(this, this.getFilesDir()+"dict.db3",null,1);
final SQLiteDatabase db = helper.getWritableDatabase(); btInsert.setOnClickListener(new OnClickListener(){ @Override
public void onClick(View v) {
String word = etWord.getText().toString();
String detail = etDetail.getText().toString();
insertData(db, word,detail);
Toast.makeText(MainActivity.this, "插入数据成功", Toast.LENGTH_LONG).show();
}
}); } private void insertData(SQLiteDatabase db, String word, String detail){
//4、执行SQL语句。
db.execSQL("insert into dict(word, detail) values(?,?)", new String[]{word,detail}); }
}
使用SQLiteHelper创建数据库并插入数据的更多相关文章
- 使用SQLiteHelper创建数据库并插入数据 分类: H1_ANDROID 2013-11-05 22:44 1398人阅读 评论(0) 收藏
参考<疯狂android讲义>8.4节P424 1.获取SQLiteDatabase实例有2种方法,一是直接new SQLiteDatabase(),另一种使用SQLiteHelper.一 ...
- MySQL命令:创建数据库、插入数据
简介: 学习mysql环境为ubantu,下面记录一些基本的创建数据库和插入数据的口令 打开MySQL 服务并使用 root 登录: --打开 MySQL 服务 sudo service mysql ...
- (大数据工程师学习路径)第四步 SQL基础课程----创建数据库并插入数据
一.练习内容 1.新建数据库 首先,我们创建一个数据库,给它一个名字,比如“mysql_shiyan”,以后的几次实验也是对mysql_shiyan这个数据库进行操作. 语句格式为“CREATE DA ...
- MySQL基础教程——创建数据库并插入数据
本节将介绍 MySQL 新建数据库,新建表,插入数据以及基本数据类型的相关知识.本节实验将创建一个名为 mysql_shiyan 的数据库,其中有两张表 employee和 department. 1 ...
- MySQL创建数据库并插入数据
启动MySql 启动服务:sudo service mysql start 登陆:mysql -u root 新建数据库 CREATE DATABASE <数据库名>; 在大多数SQL系统 ...
- linq,创建数据库,插入数据,newDB.CreateDatabase();newDB.tb2.InsertOnSubmit(stu); newDB.SubmitChanges();
using System.Data.Linq;using System.Data.Linq.Mapping; namespace ConsoleApplication1388{ class Progr ...
- 数据库中插入数据时发生ora-00984错误
操作Oracle数据库,插入数据时显示:ORA-00984列在此处不允许错误,如下图所示: 出现的原因是由于,在插入字符或字符串型字段时.如果插入的数据是纯数字,则不会有错误:如果出现字符,则会报OR ...
- 向Oracle数据库中插入数据出错:ORA-01036 无效的变量名或数据
向Oracle数据库中插入数据出错: 经过排查,因为Update数据时没有出错,所以OracleHelper没有问题: 看异常信息提示:无效的变量和数据,应该是SQL语句的问题,调试时所传的实例Use ...
- mysql数据库中插入数据INSERT INTO SET的优势
往mysql数据库中插入数据.以前常用 INSERT INTO 表名 (列名1,列名2…) VALUES(列值1,列值2); 如果在PHP程序中,就会写成如下示例(往商品库里增加商品) $sql = ...
随机推荐
- C++单链表的创建与操作
链表是一种动态数据结构,他的特点是用一组任意的存储单元(可以是连续的,也可以是不连续的)存放数据元素.链表中每一个元素成为“结点”,每一个结点都是由数据域和指针域组成的,每个结点中的指针域指向下一个结 ...
- UVA11361 Investigating Div-Sum Property(数位dp)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud 题目意思:问在区间[A,B]有多少个数不仅满足自身是k的倍数,而且其各个位数上的和 ...
- JavaScript中的原型继承原理
在JavaScript当中,对象A如果要继承对象B的属性和方法,那么只要将对象B放到对象A的原型链上即可.而某个对象的原型链,就是由该对象开始,通过__proto__属性连接起来的一串对象.__pro ...
- jQuery模糊选择
属性字头选择器(Attribute Contains Prefix Selector) jQuery 属性字头选择器的使用格式是 jQuery(‘[attribute|=value]‘) ,例如 jQ ...
- mysql innodb_double_write特性
知识储备: 1.mysql 的crasy recovery 是通过redo log 和undo log 来完成的: 2.redo log 和undo log的记录的是对页面的物理操作:如在1024号p ...
- 简单的html5布局
<!DOCTYPE html><html><meta charset="utf-8"><head><style>html ...
- 【转】如何查看linux版本 如何查看LINUX是多少位
原文网址:http://sopace.blog.51cto.com/1227753/670526 如何得知自己正在使用的linux是什么版本呢,下面的几种方法将给你带来答案! 1. 查看内核版本命令: ...
- Windows XP SP3中远程桌面实现多用户登陆
Windows XP SP3配置为支持多用户远程桌面连接,注意:此多用户远程桌面连接必须是不同的用户登录,不能像Windows server 2003那样,同一个用户可以同时登录,只能登陆2个不同用户 ...
- POJ22230 Watchcow (欧拉回路)
Watchcow Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 6477 Accepted: 2823 Specia ...
- Eclipse配置Maven开发环境
前言: 现在Eclipse版本越来越高.高版本的Eclipse甚至已经集成了Maven像是SpringSource的哪个版本.用习惯了Eclipse.在开发中还是不想更换掉自己的IDE.如此一来就又了 ...