关于android数据库的一个问题

梦不到热爱不褪 2015-05-12 10:45:12
这是一个关于android数据库的问题
1.我新建了一个DataBaseOpenHelper类继承了SQLiteOpenHelper:
package com.example.database;

import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseOpenHelper extends SQLiteOpenHelper {
private final static String DATABASE_NAME="myDb.db";
private final String TABLE_NAME="person";
private final static int VERSION=1;
private Context context;
private Cursor cursor;
public DataBaseOpenHelper(Context context) {
super(context, DATABASE_NAME, null, VERSION);
this.context=context;
this.cursor=cursor;
}

@Override
public void onCreate(SQLiteDatabase db) {
String sql="create "+TABLE_NAME+"(_id integer primary key autoincrement,name varchar(30),address varchar(30))";
try{
db.execSQL(sql);
}catch(SQLException e){
e.printStackTrace();
}
}

@Override
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2) {
// TODO Auto-generated method stub

}

}
2.然后新建一个测试类MyDataBase继承了AndroidTestCase:
package com.example.mydatabase;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.test.AndroidTestCase;
import android.util.Log;

import com.example.database.DataBaseOpenHelper;

public class MyDataBase extends AndroidTestCase {
private final String TABLE_NAME = "person";

public void insert() {
DataBaseOpenHelper mOpenHelper = new DataBaseOpenHelper(getContext());
SQLiteDatabase db = mOpenHelper.getWritableDatabase();
String sql1 = "insert into " + TABLE_NAME
+ "(_id,name,address) values('1','lisi','成都')";
String sql2 = "insert into " + TABLE_NAME
+ "(_id,name,address) values('2','zhangsan','巴中')";
String sql3 = "insert into " + TABLE_NAME
+ "(_id,name,address) values('3','wangwu','重庆')";
try {
//执行sql语句,将数据插入person表
db.execSQL(sql1);
db.execSQL(sql2);
db.execSQL(sql3);
// db.close();
} catch (SQLException e) {
e.printStackTrace();
}
}

public void Query() {
DataBaseOpenHelper mOpenHelper = new DataBaseOpenHelper(getContext());
SQLiteDatabase db = mOpenHelper.getReadableDatabase();
try {
Cursor cursor = db.query(TABLE_NAME, null, null, null, null, null,
null);
int _id=cursor.getColumnIndex("_id");
int name=cursor.getColumnIndex("name");
int address=cursor.getColumnIndex("address");

for(cursor.moveToFirst();!cursor.isAfterLast();cursor.moveToNext()){
int ID=cursor.getInt(_id);
String Name=cursor.getString(name);
String Address=cursor.getString(address);
Log.i("Query",ID+" "+Name+" "+Address);
}
} catch (SQLiteException e) {
e.printStackTrace();
}
}

}
3.问题A:
我在单元测试insert()方法时,是通过的,这时,我导出了这个myDb.db数据库文件,发现里面并没有插入数据的person表,为什么没有该表却未报异常。
问题B:
我在执行Query()方法的时候,他却报了错误,错误日志里面说的是:未找到person表。
...全文
205 2 打赏 收藏 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
装作睡不着 2015-05-13
  • 打赏
  • 举报
回复
这是正常现象吧,insert返回值可以判断插入是否成功,失败了不会报异常。查询没有该表才是异常。
源码链接: https://pan.quark.cn/s/651b74ed6067 《MQL4中文帮助手册》是一部详尽阐述MetaQuotes Language 4(MQL4)编程语言的参考书籍,其核心目标在于协助用户掌握并运用MQL4进行金融市场的自动化交易系统构建。MQL4作为MetaTrader 4交易平台的核心编程语言,赋予了交易者开发自定义指标、Expert Advisors(EA智能交易系统)以及脚本的能力。 ### MQL4基础知识 1. **Syntax 语法**:MQL4的语法规则与C/C++语言展现出相似性,涵盖了变量声明、函数调用、条件判断、循环机制等核心要素。熟练掌握并合理运用语法是开发高效MQL4代码的先决条件。 2. **Comments 注释**:在MQL4编程环境中,单行注释采用两个斜线(//)进行标识,而多行注释则通过/* 和 */进行界定。注释的主要功能在于阐释代码用途,从而提升代码的可读性与可维护性。 3. **Identifiers 标识符**:标识符是程序中用于命名变量、函数、类等元素的字符序列。其命名规则要求必须以字母或下划线作为开头,后续可包含字母、数字或下划线组合。MQL4语言对大小写敏感,因此`MyVariable`与`myvariable`被视为两个独立的标识符。 4. **Reserved words 保留字**:保留字是MQL4语言中预先设定的特殊词汇,例如`int`, `bool`, `double`, `if`, `else`, `for`, `while`等,这些词汇不可用作自定义标识符,以防止与语言关键字产生冲突。 5. **Data types 数据类型**:MQL4支持多元化的数据类型,包括...

80,489

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧