下面是最原始的方法,用SQL语句操作数据库。后面的“Android中SQLite数据库操作(2)——SQLiteOpenHelper类”将介绍一种常用的android封装操作SQLite的工具类。

MainActivity.java

package com.example.sqlitetest;

import android.app.Activity;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView; public class MainActivity extends Activity {
SQLiteDatabase db;
Button bn = null;
ListView listView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建或打开数据库
//如果my.db3存在则打开该数据库,如果不存在先创建my.db3文件再打开
/*
SQLite数据库只是一个文件
从本质上来看,SQLite的数据库操作方式是对文件的操作。
*/
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString() + "my.db3", null);
listView = (ListView) findViewById(R.id.show);
bn = (Button) findViewById(R.id.ok);
bn.setOnClickListener(new OnClickListener() { @Override
public void onClick(View source) {
//获取用户输入
EditText titleEdit = (EditText) findViewById(R.id.title);
EditText contentEdit = (EditText) findViewById(R.id.content);
String title = titleEdit.getText().toString();
String content = contentEdit.getText().toString(); try {
insertData(db, title, content); Cursor cursor = db.rawQuery("select * from news_inf", null);
inflateList(cursor);
} catch (SQLException se) {
//执行DDL创建数据表
db.execSQL("create table news_inf(_id integer" +
" primary key autoincrement," +
" news_title varchar(50)," +
" news_content varchar(255))");
//执行insert语句插入数据
insertData(db, title, content);
//执行查询
Cursor cursor = db.rawQuery("select * from news_inf", null);
inflateList(cursor);
}
}
});
} private void insertData(SQLiteDatabase db, String title, String content){
//执行插入语句
db.execSQL("insert into news_inf values(null, ?, ?)", new String[]{title, content});
} private void inflateList(Cursor cursor){
//填充SimpleCursorAdapter
SimpleCursorAdapter adapter = new SimpleCursorAdapter(MainActivity.this,
R.layout.line, cursor, new String[]{"news_title", "news_content"},
new int[]{R.id.my_title, R.id.my_content},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
listView.setAdapter(adapter);
} @Override
protected void onDestroy() {
super.onDestroy();
//退出程序时关闭SQLiteDatabase
if(db != null && db.isOpen()){
db.close();
}
}
}

string.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, DBTest!</string>
<string name="app_name">数据库访问测试</string>
<string name="insert">插入</string>
</resources>

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"
>
<EditText
android:id="@+id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="2"
/>
<Button
android:id="@+id/ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/insert"
/>
<ListView
android:id="@+id/show"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>

line.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<EditText
android:id="@+id/my_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:width="120px"
/>
<EditText
android:id="@+id/my_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>

运行中出现了错误

有错误日志可以看到是创建表时有问题,发现是字段之间忘了空格。

最后的运行结果

有关SQLite部分知识可以参考:http://blog.csdn.net/dawanganban/article/details/9832883

源代码下载:http://download.csdn.net/detail/lxq_xsyu/5890745







												

Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库的更多相关文章

  1. Django中使用mysql数据库并使用原生sql语句操作

    Django自身默认使用sqlite3这个轻量级的数据库,但是当我们开发网站时,sqlite3就没有mysql好,sqlite3适合一些手机上开发使用的数据库. 准备的软件mysql数据库,版本5.7 ...

  2. 【mybatis】mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wait timeout exceeded; try restarting transaction

    今天使用mybatis和jpa的过程中,发现这样一个问题: mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wai ...

  3. 在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作

    在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作 MyEclipse6.5    ,  mysq驱动jar包为mysql-connector ...

  4. 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】

    问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...

  5. 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建

    数据库的创建和sql语句增删改查 1. 载入驱动. 2. 连接数据库. 3. 操作数据库. 创建表: create table person( _id integer primary key, nam ...

  6. 在64位Win7中使用Navicat Premium 和PL\SQL Developer连接Oracle数据库备忘

    最近接手了一个项目,服务器端数据库是oracle 11g 64位.由于主要工作不是开发,也不想在自己的电脑上安装庞大的oracle数据库,因此寻思着只通过数据库管理工具连接数据库进行一些常用的查询操作 ...

  7. Shell脚本中执行sql语句操作mysql

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  8. Shell脚本中执行sql语句操作mysql的5种方法【转】

    对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...

  9. 在程序开发中怎样写SQL语句可以提高数据库的性能

    以下内容是公司dba总结. 1. 首先要搞明白什么叫执行计划?   执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来 ...

随机推荐

  1. 11995 - I Can Guess the Data

    大意:猜数据结构是栈.队列或者优先队列,可能为两种以上,也可能都不是. 水题.. STL 记得判断是否为空 #include<iostream> #include<cstdio> ...

  2. self.view.layer.contents 和 self.view.backgroundColor

    一. self.view.layer.contents 和 self.view.backgroundColor 今天测了一下 :    self.view.layer.contents 和 self. ...

  3. Topological Spaces(拓扑空间)

    拓扑空间的定义有多种形式,通过 open sets(开集)的形式定义是最为常见的拓扑空间定义形式. 1. 通过开集(open sets)定义 拓扑空间由一个有序对 (X,τ) 表示,X 表示非空集合, ...

  4. minizlib

    ZLIB开源库采用的是DEFLATE压缩算法,已经不支持加密功能,实际上功能还存在于代码中,采用MINIZIP可以支持对ZIP文件的加解密. ZLIB目前最新的是1.2.7,MINIZIP最新的版本是 ...

  5. OpenCV从入门到放弃(五):像素!

    一.概念 1.图像本质上面是由数值组成的矩阵.矩阵中的一个元素相应一个像素. 2.对于灰度图像(黑白图像),像素是8位无符号数(CV_8U).0表示黑色,255表示白色.对于彩色图像,是用三原色数据合 ...

  6. TF-IDF模型

    TF-IDF模型 1. 理论基础 由于数据挖掘所有数据都要以数字形式存在,而文本是以字符串形式存在.所以进行文本挖掘时需要先对字符串进行数字化,从而能够进行计算.TF-IDF就是这样一种技术,能够将字 ...

  7. thinkphp,onethink,thinkox验证码不显示

    使用验证码的时候,一开始正常,后来不显示了 网上说是utf-8的编码问题,什么bom去掉,转化为无bom的格式 我都试了,没用 后来知道是在调用验证码的地方  写上 Public function v ...

  8. [RxJS] Flatten a higher order observable with mergeAll in RxJS

    Among RxJS flattening operators, switch is the most commonly used operator. However, it is important ...

  9. STS 3.6.4 SpringMVC 4.1.6 Hibernate 4.3.8 MySQL

    开发环境: Java 1.8 Spring Tool Suite 3.6.4 Spring faramework 4.1.6 Hibernate 4.3.8 Maven 2.9 数据库是MySQL 5 ...

  10. 配置Apache支持PHP5

    环境:window 7.Apache/2.2.19 (Win32) PHP/5.2.9-1 1.配置server名称 将里面的 #ServerName localhost:80 凝视去掉就可以. 又一 ...