创建方法后如何使用SQLiteDatabase?

时间:2018-06-12 11:14:01

标签: java android sql android-sqlite

我有一个DBHelper类:

public class DBHelper extends SQLiteOpenHelper{
    public static final String DATABASE_NAME="DBHelper";
    public static final String TABLE_NAME="tblCars";
    public static final String COLUMN_MANUFACTURER="Manufacturer";
    public static final String COLUMN_YEAR="Year";
    public static final String COLUMN_HANDNUMBER="Handnumber";
    public static final String COLUMN_CARNUMBER="Carnumber";
    public static final String COLUMN_PRICE="Price";
    SQLiteDatabase db;

    public DBHelper(Context context){
        super(context, DATABASE_NAME, null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL("create table tblCars" + "(id integer primary key, manufacturer text, year text, handnumber text, carnumber text, price integer)");
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
        db.execSQL("DROP TABLE IF EXISTS tblCars");
        onCreate(db);
    }
    public void insertCar(Car c)
    {
        db = this.getWritableDatabase();
        ContentValues cv=new ContentValues();

        String query = "select * from tblCars";
        Cursor cursor=db.rawQuery(query,null);

        cv.put(COLUMN_MANUFACTURER,c.getManufacturer());
        cv.put(COLUMN_YEAR,c.getYear());
        cv.put(COLUMN_HANDNUMBER,c.getHandnumber());
        cv.put(COLUMN_CARNUMBER, c.getCarnumber());
        cv.put(COLUMN_PRICE,c.getPrice());

        db.insert(TABLE_NAME, null, cv);
        db.close();

    }
}

我希望能够允许用户添加汽车,所以我继续进行活动

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnAddCar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="25dp"
        android:text="Add Car"
        android:textAllCaps="false" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="55dp"
        android:layout_marginTop="55dp"
        android:text="Name" />

    <EditText
        android:id="@+id/etName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView2"
        android:layout_alignParentEnd="true"
        android:layout_marginEnd="38dp"
        android:ems="10"
        android:hint="Name"
        android:inputType="textPersonName"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="107dp"
        android:text="Number" />

    <EditText
        android:id="@+id/etOwnerNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView3"
        android:layout_alignStart="@+id/etName"
        android:ems="10"
        android:hint="000-000-0000"
        android:inputType="phone"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="155dp"
        android:text="Manufacturer" />

    <EditText
        android:id="@+id/etManu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView4"
        android:layout_alignStart="@+id/etName"
        android:ems="10"
        android:hint="Manufacturer"
        android:inputType="textPersonName"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginTop="202dp"
        android:text="Year" />

    <EditText
        android:id="@+id/etYear"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView5"
        android:layout_alignStart="@+id/etName"
        android:ems="10"
        android:hint="Year"
        android:inputType="number"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/textView2"
        android:layout_centerVertical="true"
        android:text="Hand no." />

    <EditText
        android:id="@+id/etHandNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignStart="@+id/etName"
        android:layout_alignTop="@+id/textView6"
        android:ems="10"
        android:hint="1,2, etc..."
        android:inputType="number"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/etCarNum"
        android:layout_alignStart="@+id/textView2"
        android:text="Car no." />

    <EditText
        android:id="@+id/etCarNum"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/etName"
        android:layout_marginBottom="171dp"
        android:ems="10"
        android:hint="Car Number"
        android:inputType="number"
        android:textSize="14sp" />

    <TextView
        android:id="@+id/textView8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignStart="@+id/textView2"
        android:layout_marginBottom="125dp"
        android:text="Price" />

    <EditText
        android:id="@+id/etPrice"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView8"
        android:layout_alignStart="@+id/etName"
        android:ems="10"
        android:hint="Price"
        android:inputType="textPersonName"
        android:textSize="14sp" />

</RelativeLayout>

我希望从中获取有关该车的信息,并将其放入一个物体(Car)中,之后我想将其存储在数据库中,但我不能使用我的&#34; insertCar&#34 ;方法由于某种原因,它一直给我错误&#34;无法解析符号insertCar&#34;。每当我尝试在任何类中使用该方法时都会发生(在这种情况下是一个新类&#34;添加&#34;)

 Car car=new Car(stManu,stYear,stHandNum,stCarNum);
 insertCar(car);

1 个答案:

答案 0 :(得分:0)

public class MainActivity extends AppCompatActivity {

    DBHelper dbHelper;
    EditText etName,etYear,etMobileNo,etManufacturer,etHandNo,etPrice,etCarNo;
    Car car;

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

    etCarNo=findViewById(R.id.etCarNum);
    etName=findViewById(R.id.etName);
    etYear=findViewById(R.id.etYear);
    etMobileNo=findViewById(R.id.etOwnerNumber;
    etManufacturer=findViewById(R.id.etManu);
    etHandNo=findViewById(R.id.etHandNum);
    etPrice=findViewById(R.id.etPrice);

    car=new Car();
    dbHelper=new DBHelper(this);

    car.setCarNo(etCarNo.getText().toString());
    car.setName(etName.getText().toString());
    car.setYear(etYear.getText().toString());
    car.setMobileNo(etMobileNo.getText().toString());
    car.setManufacturer(etManufacturer.getText().toString());
    car.setHandNo(etHandNo.getText().toString());
    car.setPrice(etPrice.getText().toString());

    dbHelper.insertCar(car);
   }
}

请试试....