安卓SQLite常见错误
利用闲时写了一个简单的Sql语句操作SQLite数据库,在用SimpleCursorAdapter时出了一个异常好久都没解决
Process: com.example.chunchuner.usesqltest405, PID: 31206
java.lang.IllegalArgumentException: column '_id' does not exist
通过一系列查找资料才发现
//使用SimpleCursorAdapter封装Cursor时候Cursor要求底层数据库的主键列的列名必须为_id
//否则会出现java.lang.IllegalArgumentException异常 代码如下:
package com.example.nanchen.usesqltest405; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter; public class MainActivity extends AppCompatActivity { private Button insert;
private EditText textView_title,textView_content;
private SQLiteDatabase db;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//创建或者打开数据库(此处需要使用绝对路径)
db = SQLiteDatabase.openOrCreateDatabase(this.getFilesDir().toString()+"/my.db3",null);
insert = (Button) findViewById(R.id.insert);
listView = (ListView) findViewById(R.id.listView);
textView_title = (EditText) findViewById(R.id.title_text);
textView_content = (EditText) findViewById(R.id.content);
insert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = textView_title.getText().toString();
String content = textView_content.getText().toString();
try {
insertData(db, title, content);
}catch (SQLiteException e){
//执行创建数据库表
db.execSQL("create table news_inf(_id integer primary key autoincrement, " +
"news_title varchar2,news_content varchar2)");
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封装Cursor时候Cursor要求底层数据库的主键列的列名必须为_id
//否则会出现java.lang.IllegalArgumentException异常
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_list_item_1,cursor,
new String[]{"news_title","news_content"},
new int[]{R.id.title_text,R.id.content},
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
//显示数据
listView.setAdapter(adapter);
} @Override
protected void onDestroy() {
super.onDestroy();
//退出程序时关闭SQLiteDataBase
if (db != null && db.isOpen()){
db.close();
}
}
}

安卓SQLite常见错误的更多相关文章
- 初识JAVA(二)(送给Java和安卓初学者)----常见错误
博主接着上篇的来讲哦,以后的更新中,博主会出一些练习题,有兴趣的可以做做然后吧代码粘贴到下面,大家可以一起研究学习,一起进步,本篇文章主要讲的是: 一.常见错误 二.连接上篇一起的训练 无论是什么方向 ...
- android安卓Sqlite数据库实现用户登录注册
看了很多别人写的安卓SQlite数据的操作代码,一点也不通俗易懂,我觉得我写的不错,而且安卓项目也用上了,所以在博客园里保存分享一下!建立一个类 并继承SQLiteOpenHelper public ...
- PHP编译安装时常见错误解决办法,php编译常见错误
PHP编译安装时常见错误解决办法,php编译常见错误 1.configure: error: xslt-config not found. Please reinstall the libxslt & ...
- php源码编译常见错误解决方案大全
php源码编译常见错误解决方案大全http://www.cnlvzi.com/index.php/Index/article/id/143 在CentOS编译PHP5的时候有时会遇到以下的一些错误信息 ...
- php编译常见错误
php PHP编译安装时常见错误解决办法[转] This article is post on https://coderwall.com/p/ggmpfa configure: error: xsl ...
- 安卓开发常见Bug-setContentView(R.layout.....)报错
这是安卓开发的常见错误,当你在引用或者复制别人的Layout xml文件时需要在AndroidManifest.xml中添加东西 需要将图中的activity android:name添加进去,否则是 ...
- ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock
ubuntu 常见错误--Could not get lock /var/lib/dpkg/lock 通过终端安装程序sudo apt-get install xxx时出错:E: Could not ...
- coreseek常见错误原因及解决方法
coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...
- Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误
嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...
随机推荐
- python 通用 修饰器
import functools def log(option): def dec(func): def swapper(*arg, **karg): functools.update_wrapper ...
- C\C++ 1A2B小游戏源码
学了一段时间,心血来潮写了一个1A2B小游戏,很多人应该玩过,是一个挺有意思的益智小游戏,之前用易语言写过,现在又用C++重写了一下. 编译运行无错,整体程序设计思路为:进入循环,初始化游戏,读入一个 ...
- dos清除缓存
ipconfig /flushdns ,DNS缓存就被清除了. ipconfig /displaydns查看DNS缓存
- 【转】WriteMessage的信息在AutoCAD中命令行中实时显示
之前程序中有段发送信息到命令行上显示的代码,如下: ed.WriteMessage("开始标注横断面高程,请稍候!"); 但是发现命令行中并不马上显示,代码也明明运 ...
- RFID工作流程
读写器通过发射天线发送一定频率的射频信号,² 当射频卡进入发射天线工作区域时产生感应电²流,射频卡获得能量被启动.²²射频卡将自身编码等信息透过卡内天线发送出²去.²²读写器接收天线接收到从 射频卡发 ...
- fwrite写入文件不成功bug
文件写入了,只是从头覆盖了!因为在fwrite():前面 文件位置指针是是SEEK_SET,即首位置.在fwrite(fileHeader, 1, 10, file); 前面加上一行fseek(fil ...
- Javascript基础回顾 之(二) 作用域
本来是要继续由浅入深表达式系列最后一篇的,但是最近团队突然就忙起来了,从来没有过的忙!不过喜欢表达式的朋友请放心,已经在写了:) 在工作当中发现大家对Javascript的一些基本原理普遍存在这里或者 ...
- 让linux好用起来--操作使用技巧
让linux好用起来--操作使用技巧 1 概述 在一个初学者眼里,linux的 CLI 界面没有图形界面那样多彩和友好,会让人产生畏难心理,但是作为一个稍微进阶的linux玩家,自然会积累不少经验 ...
- SecureCRT使用技巧
原创文章转载请注明出处:@协思, http://zeeman.cnblogs.com 修改SecureCRT颜色配置 option->Global options –> Termina ...
- 在SqlServer2008R2中,在一张表上加上insert、update、delete触发器(带游标)
在日常工作中,在SqlServer2008R2中,需要向一张表上加上触发器,监控插入.更新.删除. --一个触发器内三种INSERT,UPDATE,DELETE状态 IF exists(select ...