调用onCreate数据库时出错。 Android工作室

时间:2016-08-07 20:07:20

标签: android sql database sqlite android-studio

我已经在这里看了几个关于类似问题的不同视频和问题,我的代码看起来很好但是在我的Database类中编译 onCreate 时却无法弄清楚它为什么会出错。

我只是尝试创建一行并向数据库添加一行。

这是MainActivity

public class MainActivity extends AppCompatActivity {
    EditText First, Last, Make, Model, Cost;

Button Add;
Context dx = this;
DatabaseOps mydb;


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

    mydb = new DatabaseOps(this);

    First = (EditText) findViewById(R.id.First_Name);
    Last = (EditText) findViewById(R.id.Last_Name);
    Make = (EditText) findViewById(R.id.Car_Make);
    Model = (EditText) findViewById(R.id.Car_Model);
    Cost = (EditText) findViewById(R.id.Car_Cost);
    Add = (Button) findViewById(R.id.add);
    addData();

}
public void addData()
{
    Add.setOnClickListener(new View.OnClickListener()
    {
        @Override
        public void onClick(View v) {
            boolean ifInserted = mydb.InsertData(First.getText().toString(),
                    Last.getText().toString(),
                    Make.getText().toString(),
                    Model.getText().toString(),
                    Cost.getText().toString());
            if (ifInserted == true) {
                Toast.makeText(getBaseContext(), "Successful entry", Toast.LENGTH_LONG).show();
            } else {
                Toast.makeText(getBaseContext(), "UnSuccessful entry TRY AGAIN", Toast.LENGTH_LONG).show();
            }
        }


    });
}

}

这是数据库类

public class DatabaseOps extends SQLiteOpenHelper
{

public static final String ID = "ID";
public static final String CUSTOMER_FIRST_NAME = "Customer First Name";
public static final String CUSTOMER_LAST_NAME = "Customer Last Name";
public static final String CAR_MAKE = "Car Make";
public static final String CAR_MODEL = "Car Model";
public static final String COST = "Cost";
public static final String DATABASE_NAME = "CAR_DEALER.db";
public static final String Table_name = "CUSTOMER_TABLE";
public static final String Table_create = "create table"+ Table_name + "(" +
        ID + "INTEGER PRIMARY KEY AUTOINCREMENT"+
        CUSTOMER_FIRST_NAME +"TEXT" +
        CUSTOMER_LAST_NAME +"TEXT" +
        CAR_MAKE +"TEXT" +
        CAR_MODEL +"TEXT" +
        COST +"Integer"
        +")";



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

    Log.d("DatabaseOps", "Success Database created!" );
}

@Override
public void onCreate(SQLiteDatabase db)
{

    db.execSQL(Table_create);
    Log.d("DatabaseOps", "Success Table created!" );


}

@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
    db.execSQL("Drop Table IF Exists" +Table_name);
    onCreate(db);
}

public boolean InsertData (String First, String Last, String Make, String Model, String Cost)
{
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(CUSTOMER_FIRST_NAME, First);
    cv.put(CUSTOMER_LAST_NAME, Last);
    cv.put(CAR_MAKE, Make);
    cv.put(CAR_MODEL, Model);
    cv.put(COST, Cost);
    Long result = db.insert(Table_name, null, cv);
    if( result == -1)
    {
        return false;
    }
    else
    {
        return true;
    }
}


}

我认为清单文件没有初始化数据库类可能是一个问题,但清单显示类中没有默认构造函数。

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name="MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>




    </application>

1 个答案:

答案 0 :(得分:1)

您的问题完全取决于angular .module('app') .component('appProductList', { templateUrl: '/src/product-list/product-list.component.html', controller: ProductListController }); class ProductListController{ products: Array<Product>; constructor(private $location: ng.ILocationService, private ProductService: ProductService) {} $onInit() { this.products = new Array<Product>(); this.ProductService.getProducts() .then((res: ng.IHttpPromiseCallbackArg<Array<Product>>) => { this.products = res.data; }, error => console.log(error)); } selectProduct(product){ this.$location.path('/product/' + product.id); } } 和您的专栏名称的内容。

首先,您需要关键词和值之间的空格。 Table_create解析为"create table" + Table_name

其次,您不应在列名中添加空格。因此,而不是"create tableCUSTOMER_TABLE"使用"Car Model"

只需打印字符串即可。有关详细信息,请参阅create-table-stmt