Android中SQLite数据库操作(1)——使用SQL语句操作SQLite数据库
下面是最原始的方法,用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数据库的更多相关文章
- Django中使用mysql数据库并使用原生sql语句操作
Django自身默认使用sqlite3这个轻量级的数据库,但是当我们开发网站时,sqlite3就没有mysql好,sqlite3适合一些手机上开发使用的数据库. 准备的软件mysql数据库,版本5.7 ...
- 【mybatis】mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wait timeout exceeded; try restarting transaction
今天使用mybatis和jpa的过程中,发现这样一个问题: mybatis执行一个update方法,返回值为1,但是数据库中数据并未更新,粘贴sql语句直接在数据库执行,等待好久报错:Lock wai ...
- 在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作
在myeclipse中配置DB Driver(数据库用MySql),并在myeclipse执行sql语句操作 MyEclipse6.5 , mysq驱动jar包为mysql-connector ...
- 【mybatis】service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据【事务的问题】
问题描述: service层中一个方法中使用mybatis进行数据库的 多个修改操作,可能是update也可能是delete操作,但是sql语句命名执行并且在控制台打印出来了,但是数据库中未更新到数据 ...
- 【黑马Android】(04)数据库的创建和sql语句增删改查/LinearLayout展示列表数据/ListView的使用和BaseAdater/内容提供者创建
数据库的创建和sql语句增删改查 1. 载入驱动. 2. 连接数据库. 3. 操作数据库. 创建表: create table person( _id integer primary key, nam ...
- 在64位Win7中使用Navicat Premium 和PL\SQL Developer连接Oracle数据库备忘
最近接手了一个项目,服务器端数据库是oracle 11g 64位.由于主要工作不是开发,也不想在自己的电脑上安装庞大的oracle数据库,因此寻思着只通过数据库管理工具连接数据库进行一些常用的查询操作 ...
- Shell脚本中执行sql语句操作mysql
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- Shell脚本中执行sql语句操作mysql的5种方法【转】
对于自动化运维,诸如备份恢复之类的,DBA经常需要将SQL语句封装到shell脚本.本文描述了在Linux环境下mysql数据库中,shell脚本下调用sql语句的几种方法,供大家参考.对于脚本输出的 ...
- 在程序开发中怎样写SQL语句可以提高数据库的性能
以下内容是公司dba总结. 1. 首先要搞明白什么叫执行计划? 执行计划是数据库根据SQL语句和相关表的统计信息作出的一个查询方案,这个方案是由查询优化器自动分析产生的,比如一条SQL语句如果用来 ...
随机推荐
- 硬件——STM32 , 录音
战舰V3的录音程序解析 上一章,我们实现了一个简单的音乐播放器,本章我们将在上一章的基础上,实现一个简单的录音机,实现WAV录音.本章分为如下几个部: 50.1 WAV简介 50.2 硬件设计 50. ...
- UVA 11609 - Teams 组合、快速幂取模
看题传送门 题目大意: 有n个人,选一个或者多个人参加比赛,其中一名当队长,如果参赛者相同,队长不同,也算一种方案.求一共有多少种方案. 思路: 排列组合问题. 先选队长有C(n , 1)种 然后从n ...
- [WASM] Create and Run a Native WebAssembly Function
In this introduction, we show a simple WebAssembly function that returns the square root of a number ...
- FastSocket学习笔记~RPC的思想,面向对象的灵活
首先非常感谢这位来自新浪的老兄,它开发的这个FastSocket非常不错,先不说性能如何,单说它的使用方式和理念上就很让人赞口,从宏观上看,它更像是一种远程过程的调用RPC,即服务器公开一些命令,供客 ...
- matplotlib学习之颜色样式
一.颜色 1.内建八种默认颜色 蓝色 - 'b' 绿色 - 'g' 红色 - 'r' 青色 - 'c' 品红 - 'm' 黄色 - 'y' 黑色 - 'k' 白色 - 'w' 2.灰度 plt.plo ...
- Linux有问必答:Linux上如何查看某个进程的线程
原创:LCTT https://linux.cn/article-5633-1.html 译者: GOLinux本文地址:https://linux.cn/article-5633-1.html201 ...
- 那些移动端web踩过的坑
原文链接:https://geniuspeng.github.io/2017/08/24/mobile-issues/ 扔了N久,还是捡回来了.好好弄一下吧.刚工作的时候挺忙的,后来不那么忙了,但是变 ...
- struts2-token防止重复提交解决办法
1.配置struts.xml全局防重复提交拦截器栈: <struts> <package name="module" extends="struts-d ...
- Java算法--串的简单处理
题目例如以下: 串的处理 在实际的开发工作中.对字符串的处理是最常见的编程任务. 本题目即是要求程序对用户输入的串进行处理.详细规则例如以下: 1. 把每个单词的首字母变为大写. 2. 把数字与字母之 ...
- ios开发网络学八:NSURLSession相关代理方法
#import "ViewController.h" @interface ViewController ()<NSURLSessionDataDelegate> /* ...