- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在尝试复制我用 SQLite 管理器创建的数据库,我在其中做了:
CREATE TABLE "android_metadata" ("locale" TEXT DEFAULT 'en_US')
和
INSERT INTO "android_metadata" VALUES ('en_US')
我将所有主键命名为 _id
。我的数据库被复制(在第一次运行时,logcat 中有各种红色消息);此后,它只会在我查询时出错。
MainActivity
public class MainActivity extends Activity {
String CNAME=" ques",TABLE_NAME=" JAVAQ";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Setupdb dbobj=new Setupdb(this);
try {
//dbobj.close();
dbobj.createDataBase();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dbobj.openDataBase();
dbobj.close();
try{
SQLiteDatabase sqdb=dbobj.getReadableDatabase();
Cursor c = sqdb.query(TABLE_NAME,
new String[] { CNAME },
null, null, null, null, null);
while (c.moveToNext()) {
String name =
c.getString(c.getColumnIndex(CNAME));
Log.i("LOG_TAG", " HAS NAME " + name);
}}
catch(Exception e){
Log.e("err", e.toString());
}
}}
安装数据库
public class Setupdb extends SQLiteOpenHelper {
private static String DB_PATH = "";
private static final String DB_NAME = "camprep.sqlite";
private SQLiteDatabase myDataBase;
private final Context myContext;
private static Setupdb mDBConnection;
public Setupdb(Context context) {
super(context, DB_NAME, null, 3);
this.myContext=context;
DB_PATH="/data/data/"
+ context.getApplicationContext().getPackageName()
+ "/databases/";
Log.e(DB_NAME, DB_PATH);
}
public static synchronized Setupdb getDBAdapterInstance(Context context) {
if (mDBConnection == null) {
mDBConnection = new Setupdb(context);
}
return mDBConnection;
}
public void createDataBase() throws IOException {
boolean dbExist = checkDataBase();
if (dbExist) {
Log.e("db","exist");
// do nothing - database already exist
} else {
// By calling following method
// 1) an empty database will be created into the default system path of your application
// 2) than we overwrite that database with our database.
this.getReadableDatabase();
try {
Log.e("calling", "copy");
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private boolean checkDataBase() {
SQLiteDatabase checkDB = null;
try {
String myPath = DB_PATH + DB_NAME;
checkDB = SQLiteDatabase.openDatabase(myPath, null,
SQLiteDatabase.OPEN_READONLY);
} catch (SQLiteException e) {
// database does't exist yet.
}
if (checkDB != null) {
checkDB.close();
}
return checkDB != null ? true : false;
}
private void copyDataBase() throws IOException {
// Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
// Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
// transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
myOutput.write(buffer, 0, length);
}
// Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
public void openDataBase() throws SQLException {
String myPath = DB_PATH + DB_NAME;
myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
}
public synchronized void close() {
if (myDataBase != null)
myDataBase.close();
super.close();
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
堆栈跟踪
08-31 20:17:05.320: I/dalvikvm(9457): threadid=3: reacting to signal 3
08-31 20:17:05.370: I/dalvikvm(9457): Wrote stack traces to '/data/anr/traces.txt'
08-31 20:17:05.451: E/camprep.sqlite(9457): /data/data/com.example.mydataexplosion/databases/
08-31 20:17:05.490: E/db(9457): exist
08-31 20:17:05.521: E/CursorWindow(9457): Failed to read row 0, column -1 from a CursorWindow which has 11 rows, 1 columns.
08-31 20:17:05.521: E/err(9457): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-31 20:17:05.650: D/gralloc_goldfish(9457): Emulator without GPU emulation detected.
08-31 20:17:05.650: I/dalvikvm(9457): threadid=3: reacting to signal 3
08-31 20:17:05.670: I/dalvikvm(9457): Wrote stack traces to '/data/anr/traces.txt'
最佳答案
如果你看到
failed to read row 0,column -1
这意味着您正在尝试从不存在的列中读取。
如果找不到您指定的列名,Cursor.getColumnIndex()
返回 -1
,因此无效。
这有两个原因:
注意:使用getColumnIndex()
在你的场景中:
c.getString(c.getColumnIndex(CNAME));
检查 CNAME 变量的拼写是否正确,以及该名称的列是否存在。
String CNAME=" ques"
例如,是否应该有额外的前导空白..
关于android - 未能读取第 0 行第 -1 列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12222141/
我很快就会明白,我不是 Git 甚至 Gitkraken 的高手。因此,我只有一个修补程序、一个主分支和一个功能分支。我在修补程序、提交、推送和 merge 到 master 中进行更改(然后我也推送
我刚开始使用 stub 请求来测试对 iOS 的外部 API 的异步调用。我目前被以下代码困住了,我无法弄清楚什么不起作用。 我想要实现的非常简单的事情是,如果我从网站收到 200 响应,我将 Vie
设置: 一个 JPA ReviewRepository延长 CrudRepository 我的测试使用切片测试注释 @DataJpaTest 我的测试@Autowired ReviewReposito
我尝试通过logstash将csv文件vrom filebeat摄取到hdfs中。 Filebeat 成功将其转移到 logstash,因为我使用 stdout{codec=>rubydebug} 并
我看到很多教程解释了如何在 Tensorflow 的 Bazel WORKSPACE 中构建项目(例如 this one)。但我似乎无法找到一种方法来构建我自己的项目并将 tensorflow 作为依
我正在运行 Ubuntu 10.04 并且最初安装了 ruby 1.9.1(来自源代码)。我刚刚决定试用 ruby 1.9.2 和 rails 3,现在似乎是使用 rvm 处理多个 ruby
我有一个应用程序从后端接收支持的语言环境列表作为以下响应: {locales: [{code: 'enUS'}, {code: 'deDE'}, {code: 'arAR'}]} 我想使用 date-
我是一名优秀的程序员,十分优秀!