我得到android:android.database.sqlite.SQLiteException
无法将只读数据库从版本0升级到1:/data/data/com.tuto.ListViewApp2_SQLite/databases/countryDB
当我运行模拟器它没有启动时,它说"不幸的是appName已停止"所以身体能帮助我吗?
这是我的MainActivity类:
package com.tuto.ListViewApp2_SQLite;
import com.tuto.ListViewApp2_SQLite.R;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.os.Bundle;
import android.provider.BaseColumns;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class MainActivity extends Activity {
private CountryDB countryDatabase = null;
private Cursor countries = null;
private ListView lvCountries = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Create the database helper
CursorFactory factory = null;
countryDatabase = new CountryDB(this, "countryDB", factory);
String[] columnNames = {"Name", "Pop", "Area", BaseColumns._ID};
lvCountries = (ListView) findViewById(R.id.lvCountries);
int[] targetLayoutIDs = {R.id.textName, R.id.textPop, R.id.textArea};
SQLiteDatabase db = countryDatabase.getReadableDatabase();
countries = db.query("countries", columnNames, null, null, null, null, null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.country_item,
countries, columnNames, targetLayoutIDs, 0);
lvCountries.setAdapter(adapter);
}
@Override
protected void onPause() {
super.onPause();
countryDatabase.close();
}
}
这是我的SQLiteOpenHelper的子类:
package com.tuto.ListViewApp2_SQLite;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class CountryDB extends SQLiteOpenHelper {
private static final int DB_VERSION = 1;
private static final String COUNTRY_TABLE_NAME = "countries";
public CountryDB(Context context, String name, CursorFactory factory) {
super(context, name, factory, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase dbCountries) {
createTable(dbCountries);
}
@Override
public void onUpgrade(SQLiteDatabase dbCountries, int oldVersion, int newVersion) {
String dropSQL = "DROP TABLE IF EXISTS " + COUNTRY_TABLE_NAME +";";
dbCountries.execSQL(dropSQL);
createTable(dbCountries);
}
private void createTable(SQLiteDatabase dbCountries){
String createSQL = "CREATE TABLE "+COUNTRY_TABLE_NAME+" ("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT, "+
"Name TEXT, "+
"Pop TEXT, "+
"Area TEXT);";
dbCountries.execSQL(createSQL);
String insertSQL = "INSERT INTO " + COUNTRY_TABLE_NAME +
" (Name, Pop, Area) " +
"SELECT 'South Korea' AS NAME, '41,769,726' AS Pop, '1,068,296' AS Area"+
" UNION SELECT 'Australia', '21,766,711', '2,967,893'" +
" UNION SELECT 'India' , '1,189,172,906', '1,269,338',+" +
" UNION SELECT 'United States', '313,232,044', '3,718,691';";
dbCountries.execSQL(insertSQL);
}
}
答案 0 :(得分:4)
您的insertSQL
未执行且失败。这就是您的数据库未升级的原因。 insert查询在第二个最后一个语句中有一个逗号和一个加号,使查询无效。
试试这个 -
String insertSQL = "INSERT INTO " + COUNTRY_TABLE_NAME +
" (Name, Pop, Area) " +
"SELECT 'South Korea' AS NAME, '41,769,726' AS Pop, '1,068,296' AS Area"+
" UNION SELECT 'Australia', '21,766,711', '2,967,893'" +
" UNION SELECT 'India' , '1,189,172,906', '1,269,338'" +
" UNION SELECT 'United States', '313,232,044', '3,718,691';";
答案 1 :(得分:2)
您的文件系统上已有只读数据库。它的版本是0.您正在尝试将其升级到版本1.问题是它只读,所以您无法改变它。最简单的解决方案是卸载并重新安装应用程序,以摆脱旧的应用程序。或者检查是否存在旧数据库并首先将其删除。这些中的任何一个都会丢失其中的数据,但是根据您的onUpgrade代码,无论如何都可以。但