我尝试创建一个数据库并尝试填充它,但是字段“ price”不是错误,我显示的代码以及常量Util.KEY_PRICE的状态不是这样,请帮助boloe谢谢。我第一次使用Android Studio中的数据库在教室中通过视频进行操作,但是没有办法。AndroidStudio版本:3.4.2
尝试添加此
SQLiteDatabase db = databaseHandler.getWritableDatabase(); in
MainActivity.java in onCreate ,don't work
public class DatabaseHandler extends SQLiteOpenHelper {
public DatabaseHandler(Context context) {
super(context, Util.DATABASE_NAME, null, Util.DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//SQL - structured query language (язык структурированных запросов)
String CREATE_CARS_TABLE = "CREATE TABLE "+Util.TABLE_NAME+"("
+ Util.KEY_ID + " INTEGER PRIMARY KEY,"
+ Util.KEY_NAME + " TEXT,"
+ Util.KEY_PRICE + " TEXT" + ")";
db.execSQL(CREATE_CARS_TABLE);
//public static final String KEY_PRICE = "price";
}
MainActivity.java:
import androidx.appcompat.app.AppCompatActivity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import java.util.List;
import Data.DatabaseHandler;
import Model.Car;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DatabaseHandler databaseHandler = new DatabaseHandler(this);
//SQLiteDatabase db = databaseHandler.getWritableDatabase();
databaseHandler.addCar(new Car("Toyota","30 000 $"));
databaseHandler.addCar(new Car("Opel","25 000 $"));
databaseHandler.addCar(new Car("Mersedes","50 000 $"));
databaseHandler.addCar(new Car("KIA","28 000 $"));
databaseHandler.addCar(new Car("Mazda","30 000 $"));
databaseHandler.addCar(new Car("Honda","25 000 $"));
databaseHandler.addCar(new Car("Skoda","50 000 $"));
databaseHandler.addCar(new Car("Hundai","28 000 $"));
List<Car> carList = databaseHandler.getAllCars();
for (Car car : carList){
Log.d("CarInfo : ","ID " + car.getId()+ ", name " + car.getName()+
",price " + car.getPrice());
}
}
}
10-02 13:31:44.731 6356-6356/com.kozlov_v_s_2019.carsdb E/SQLiteDatabase: Error inserting name=Toyota price=30 000 $
android.database.sqlite.SQLiteException: table cars has no column named price (code 1): , while compiling: INSERT INTO cars(name,price) VALUES (?,?)
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341)
at Data.DatabaseHandler.addCar(DatabaseHandler.java:43)
at com.kozlov_v_s_2019.carsdb.MainActivity.onCreate(MainActivity.java:22)
at android.app.Activity.performCreate(Activity.java:5990)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2280)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2392)
at android.app.ActivityThread.access$800(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1305)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5293)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)