I am getting android: android.database.sqlite.SQLiteException
Can't upgrade read-only database from version 0 to 1: /data/data/com.tuto.ListViewApp2_SQLite/databases/countryDB
When i run the emulator it does not start, it said "unfortuntly appName has stopped" so can any body help me ?
This is my MainActivity class:
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();
}
}
And this my subclass of 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);
}
}
Aucun commentaire:
Enregistrer un commentaire