0

I have looked at several different videos and questions on here about similar issues and my code seems to be fine but just can not figure out why it has an error when compiling onCreate in my Database class.

I am only attempting to create and add a row to the database.

This is the 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();
            }
        }


    });
}

}

This is the Database class

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


}

I was thinking it could be an issue with the manifest file not initializing the database class, however the manifest says there was no default constructor in the class.

<?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 Answer 1

1

Your issue is completely with the contents of Table_create and your column names.

First you need spaces between key words and values. "create table" + Table_name resolves to "create tableCUSTOMER_TABLE".

Second you should not put spaces in your column names. So instead of "Car Model" use "Car_Model" for example.

It helps, just to print the string. See create-table-stmt for more information.

Sign up to request clarification or add additional context in comments.

1 Comment

You also need commas after each type.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.