下面是最原始的方法,用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. Maven报错Missing artifact jdk.tools:jdk.tools:jar:1.7--转

    原文地址:http://blog.csdn.net/u013281331/article/details/40824707 在Eclipse中检出Maven工程,一直报这个错:“Missing art ...

  2. 关于IO重定向

    首先,Unix进程使用文件描述符0,1,2作为标准输入.输出和错误的通道. 其次,当进程请求一个新的文件描述符的时候,系统内核将最低可用的文件描述符赋给它. 第三,文件描述符集合通过exec调用传递, ...

  3. [Ramda] Pluck & Props: Get the prop(s) from object array

    Pluck: Get one prop from the object array: R.pluck(}, {a: }]); //=> [1, 2] R.pluck()([[, ], [, ]] ...

  4. Qt浅谈之一:内存泄露(总结),对于QWidget可以setAttribute(Qt::WA_DeleteOnClose),而且绝对不能手动删除栈上的对象

    一.简介 Qt内存管理机制:Qt 在内部能够维护对象的层次结构.对于可视元素,这种层次结构就是子组件与父组件的关系:对于非可视元素,则是一个对象与另一个对象的从属关系.在 Qt 中,在 Qt 中,删除 ...

  5. 15.1 linux操作系统下nand flash驱动框架2

    当我们需要在操作系统上读写普通文件的时候,总是需要一层层往下,最终到达硬件相关操作,当然底层设备大多数都是块设备 NAND FLASH就作为一个最底层的块设备. 而写驱动,就是要构建硬件与操作系统之间 ...

  6. [Angular2] @Ngrx/store and @Ngrx/effects learning note

    Just sharing the learning experience related to @ngrx/store and @ngrx/effects. In my personal opinio ...

  7. leetcode笔记:Remove Duplicates from Sorted Array II

    一.题目描写叙述 二.解题技巧 这道题和Remove Duplicates from Sorted Array这道题是相似的.仅仅只是这里同意出现反复的数字而已,能够採用二分搜索的变种算法.仅仅只是增 ...

  8. iOS将汉字转换为拼音

    将汉字转换为拼音 - (NSString *)chineseToPinyin:(NSString *)chinese withSpace:(BOOL)withSpace { CFStringRef h ...

  9. [Math Processing Error] 问题的解决(F5刷新页面与 Ctrl/Shift + F5 刷新页面的区别)

    Why is [Math Processing Error] all over the place today? 当打开某页面出现 [Math Processing Error],一般表示 MathJ ...

  10. Android中常用的优秀开源框架

    Android开源框架库分类,挑选出最常用,最实用的开源项目,本篇主要介绍的是优秀开源框架库和项目,UI个性化控件会独立介绍.UI个性化控件 Index Dependency Injections A ...