利用闲时写了一个简单的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常见错误的更多相关文章

  1. 初识JAVA(二)(送给Java和安卓初学者)----常见错误

    博主接着上篇的来讲哦,以后的更新中,博主会出一些练习题,有兴趣的可以做做然后吧代码粘贴到下面,大家可以一起研究学习,一起进步,本篇文章主要讲的是: 一.常见错误 二.连接上篇一起的训练 无论是什么方向 ...

  2. android安卓Sqlite数据库实现用户登录注册

    看了很多别人写的安卓SQlite数据的操作代码,一点也不通俗易懂,我觉得我写的不错,而且安卓项目也用上了,所以在博客园里保存分享一下!建立一个类 并继承SQLiteOpenHelper public ...

  3. PHP编译安装时常见错误解决办法,php编译常见错误

    PHP编译安装时常见错误解决办法,php编译常见错误 1.configure: error: xslt-config not found. Please reinstall the libxslt & ...

  4. php源码编译常见错误解决方案大全

    php源码编译常见错误解决方案大全http://www.cnlvzi.com/index.php/Index/article/id/143 在CentOS编译PHP5的时候有时会遇到以下的一些错误信息 ...

  5. php编译常见错误

    php PHP编译安装时常见错误解决办法[转] This article is post on https://coderwall.com/p/ggmpfa configure: error: xsl ...

  6. 安卓开发常见Bug-setContentView(R.layout.....)报错

    这是安卓开发的常见错误,当你在引用或者复制别人的Layout xml文件时需要在AndroidManifest.xml中添加东西 需要将图中的activity android:name添加进去,否则是 ...

  7. 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 ...

  8. coreseek常见错误原因及解决方法

    coreseek常见错误原因及解决方法 Coreseek 中文全文检索引擎 Coreseek 是一款中文全文检索/搜索软件,以GPLv2许可协议开源发布,基于Sphinx研发并独立发布,专攻中文搜索和 ...

  9. Android Fragment使用(二) 嵌套Fragments (Nested Fragments) 的使用及常见错误

    嵌套Fragment的使用及常见错误 嵌套Fragments (Nested Fragments), 是在Fragment内部又添加Fragment. 使用时, 主要要依靠宿主Fragment的 ge ...

随机推荐

  1. centos上安装pygame

    安装前依赖包检查及安装 python-devel SDL_image-devel SDL_mixer-devel SDL_ttf-devel SDL-devel numpy subversion po ...

  2. Xcode 调试技巧

    一 NSLog调试 官方文档:Logs an error message to the Apple System Log facility. 即NSLog不是作为普通的debug log的,而是err ...

  3. Ajax全面基础学习(二)

    两种配置ajax的方式 $.ajax('url',{ 配置ajax}); $.ajax({ url : 'url' 其他ajax配置}) ajax的回调函数 $.ajax('url',{ //请求成功 ...

  4. MySQL 注册码

    最近开发是用MySQL数据库,之前安装后,每次打开Navicat Premium 都提示 “试用一个月”,“ 注册”,然后为了方便,就都选择了 试用的, 一个月后,就必须的输入注册码,(还有一个是 什 ...

  5. 详解Java GC的工作原理+Minor GC、FullGC

    详解Java GC的工作原理+Minor GC.FullGC 引用地址:http://www.blogjava.net/ldwblog/archive/2013/07/24/401919.html J ...

  6. 使用 MimeKit 和 MailKit 发送邮件

    MimeKit 给.NET 社区带来了一流的 MIME 解析器,它能处理复杂的各种Mime, 性能好.而且开箱即用支持 S/MIME 和 PGP.MimeKit 和 MailKit 支持最新的国际化的 ...

  7. ABP理论学习之应用服务

    返回总目录 本篇目录 IApplicationService接口 ApplicationService类 工作单元 数据库连接和事务管理 自动保存更改 更多 应用服务的生命周期 应用服务用于将领域逻辑 ...

  8. ABP理论学习之审计日志

    返回总目录 本篇目录 介绍 配置 通过特性开启/关闭 注意 我项目中的例子 介绍 维基百科说: "审计跟踪(也叫审计日志)是与安全相关的按照时间顺序的记录,记录集或者记录源,它们提供了活动序 ...

  9. 你必须知道的指针基础-7.void指针与函数指针

    一.不能动的“地址”—void指针 1.1 void指针初探 void *表示一个“不知道类型”的指针,也就不知道从这个指针地址开始多少字节为一个数据.和用int表示指针异曲同工,只是更明确是“指针” ...

  10. c#实现redis客户端(一)

    最近项目使用中要改造redis客户端,看了下文档,总结分享一下. 阅读目录: 协议规范 基础通信 状态命令 set.get命令 管道.事务 总结 协议规范 redis允许客户端以TCP方式连接,默认6 ...