Ilazar1x Ilazar1x - 1 month ago 9
Android Question

I get FATAL EXCEPTION: table cars has no column named year

Can find why i get this error can someone help?

package Android.data;

public class CarsDbConstants
{

public static final String DATABASE_NAME = "cars.db";

public static final int DATABASE_VERSION = 1;
public static final String CARS_TABLE_NAME = "cars";

public static final String CAR_ID = "_id";
public static final String CAR_MANUFACTURER = "manufacturer";
public static final String CAR_YEAR= "year";
public static final String LOG_TAG = "CarsDb";

}





package Android.data;

public class CarsDBHelper extends SQLiteOpenHelper
{
public CarsDBHelper(Context context, String name, CursorFactory factory,
int version)
{
super(context, name, factory, version);
}

@Override
public void onCreate(SQLiteDatabase db)
{
Log.d(LOG_TAG, "Creating all the tables");

String CREATE_CARS_TABLE =
"CREATE TABLE " + CARS_TABLE_NAME +
"(" + CAR_ID + " INTEGER PRIMARY KEY,"
+ CAR_MANUFACTURER + " TEXT,"
+ CAR_YEAR + " INTEGER"+")";
try
{
db.execSQL(CREATE_CARS_TABLE);
}
catch (SQLiteException ex)
{
Log.e(LOG_TAG, "Create table exception: " + ex.getMessage());
}
}
}





public void addCar(Cars car)
{
db = dbhelper.getWritableDatabase();

ContentValues newCarValues = new ContentValues();
//newCarValues.put(CAR_ID,car.get_id());
newCarValues.put(CAR_MANUFACTURER, car.getManufacturer());
newCarValues.put(CAR_YEAR, car.getYear());


// Inserting the new row, or throwing an exception if an error occurred
try
{
db.insertOrThrow(CARS_TABLE_NAME, null, newCarValues);
}
catch (SQLiteException ex)
{
Log.e(LOG_TAG, ex.getMessage());
throw ex;
}
finally
{
db.close();
}
}





package Android.hwsqldb;

public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

CarsHandler db = new CarsHandler(this);
insertCars(db);
Cursor cursor = db.getAllCars();
this.startManagingCursor(cursor);
showCars(cursor);
}



private void showCars(Cursor c) {
Log.d("Reading:", "Reading all cars");

ArrayList<String> items = new ArrayList<String>();

while (c.moveToNext()) {
items.add(String.format("%s: %s\n\t%s, %s",
c.getInt(c.getColumnIndex(CAR_ID)),
c.getString(c.getColumnIndex(CAR_MANUFACTURER)),
c.getInt(c.getColumnIndex(CAR_YEAR))));
}

ListView lv = (ListView) findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lv.setAdapter(adapter);
}





private void insertCars(CarsHandler db)
{
Log.d("Inserting:", "Inserting cars to DB");

db.addCar(new Cars(1,"Subaru", 1987));
db.addCar(new Cars(2,"Fiat", 1921));
db.addCar(new Cars(3,"Susita", 1956));
db.addCar(new Cars(4,"Mesrsedes", 1923));
}


Error Thrown:

07-29 07:08:47.315: E/AndroidRuntime(1165): FATAL EXCEPTION: main

07-29 07:08:47.315: E/AndroidRuntime(1165): java.lang.RuntimeException: Unable to start activity ComponentInfo{Android.hwsqldb/Android.hwsqldb.MainActivity}: java.util.MissingFormatArgumentException: Format specifier: s


DB table created contains all data.

Answer

public static final String CAR_YEAR= "year";

You can change the column name like

public static final String CAR_YEAR= "_year";

then test your code and check database and table is created or not from android DDMS file Explorer.

your code block ::

@Override public void onCreate(Cdb) { Log.d(LOG_TAG, "Creating all the tables");

what is this parameter 'SQLiteDatabase db' . It is accepted to pass SQLiteDatabase parameter into onCreate() method.

You can call SQLiteDatabase class.

SQLiteDatabase db = new SQLiteDatabase () into onCreate() method