下面是最原始的方法,用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. jtag引脚

    如果不能下载,可能原因也许是电量不足了... 在电力不足的时候,仿真也不能进行... ///////////////////////////////////////////////////////// ...

  2. C++11实现placeholder

    文章分析怎样在C++11中实现简单的placeholder. 首先看看什么是placeholder: for_each(arr.begin(), arr.end(), cerr << _0 ...

  3. POJ 3211 Washing Clothes 0-1背包

    题目大意: xxx很懒,但他有个漂亮又勤奋的女友 (尼玛能不能不刺激我,刚看到这题的时候发现自己的衣服没洗!!!) 可以帮他洗衣服. 洗衣服的时候要求不同的颜色的衣服不能同时洗.一人洗一件的话,问最短 ...

  4. 关于重定向RedirectAttributes的用法(转)

    原文地址:https://zhuanlan.zhihu.com/p/21353217?refer=pengsong-java RedirectAttributes 是Spring mvc 3.1版本之 ...

  5. [PostgreSQL] Use Foreign Keys to Ensure Data Integrity in Postgres

    Every movie needs a director and every rented movie needs to exist in the store. How do we make sure ...

  6. 移动web处理input输入框输入银行卡号四位一空格

    由于项目上有需求要求输入银行卡号四位一空格的需求,改过好几版发现都有bug,最后优化了一版看起来效果还行,发帖留存. 难点是从中间插入和删除处理光标问题. 首先需要用到获取光标和设置光标的方法. // ...

  7. 洛谷 P1709 隐藏口令Hidden Password

    ->题目链接 题解: 贪心+字符串 #include<iostream> #include<cstring> #define N 5000005 using namesp ...

  8. iOS开发网络学习七:NSURLSession的基本使用get和post请求

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  9. ios开发之Quartz2D 四:画饼图

    #import "PieView.h" @implementation PieView - (void)drawRect:(CGRect)rect { // Drawing cod ...

  10. kernel-char设备的建立

    kernel下的设备分成了一些类,char block char.. 这里就贴出来一个样例能够建立一个char设备 ,抛砖引玉吧 这是kernel中的 drivers/char/msm_smd_pkt ...