-1

可以找到为什么我得到这个错误有人可以帮忙吗?

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));   
}

抛出的错误:

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

创建的数据库表包含所有数据。

4

1 回答 1

0

公共静态最终字符串CAR_YEAR =“年”;

您可以更改列名,例如

公共静态最终字符串CAR_YEAR =“_year”;

然后测试您的代码并检查是否从 android DDMS 文件资源管理器创建了数据库和表。

你的代码块::

@Override public void onCreate(Cdb) { Log.d(LOG_TAG, "创建所有表");

这个参数 'SQLiteDatabase db' 是什么。接受将 SQLiteDatabase 参数传递给 onCreate() 方法。

您可以调用 SQLiteDatabase 类。

SQLiteDatabase db = new SQLiteDatabase() 进入 onCreate() 方法

于 2012-07-29T06:25:03.193 回答