Я пытаюсь создать базу данных и таблицу для хранения данных моей карты, а затем отобразить их. И я создал эту схему
public class MyDatabaseHelper extends SQLiteOpenHelper {
private Context context;
private static String DB_NAME="CardsDetails.db";
private static final int DB_VERSION=1;
private static final String table_name="my_cards";
private static final String id="_id";
private static final String card_num="Card Number";
private static final String cvv="cvv";
private static final String year="year";
private static final String month="month";
private static final String user_name="Name on Card";
public MyDatabaseHelper(@Nullable Context context) {
super(context,DB_NAME,null,DB_VERSION);
this.context=context;
}
@Override
public void onCreate(SQLiteDatabase db) {
String query= "CREATE TABLE "+table_name+" ("+id+" INTEGER PRIMARY KEY AUTOINCREMENT, "+"card_num"+" TEXT, "+month+" TEXT, "+year+" TEXT, "+cvv+" TEXT, "+user_name+" TEXT);";
db.execSQL(query);
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1) {
db.execSQL("DROP TABLE IF EXISTS "+table_name);
onCreate(db);
}
void addCard(String CardNo, String mon_, String year_, String cvv_, String name)
{
SQLiteDatabase db=this.getWritableDatabase();
ContentValues cv=new ContentValues();
cv.put(card_num,CardNo);
cv.put(month,mon_);
cv.put(year,year_);
cv.put(cvv,cvv_);
cv.put(user_name,name);
long result=db.insert(table_name,null,cv);
if(result==-1)
Toast.makeText(context,"Input Failed!",Toast.LENGTH_LONG).show();
else
Toast.makeText(context,"Card Saved Successfully",Toast.LENGTH_LONG).show();
}
}
Но я получаю некоторые ошибки и исключения в этом коде, которые я не понимаю. Вот отрывок из журнала.
E/SQLiteLog: (1) near "on": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.antas.fingerprintauth, PID: 22200
android.database.sqlite.SQLiteException: near "on": syntax error (code 1 SQLITE_ERROR): , while compiling: CREATE TABLE my_cards (_id INTEGER PRIMARY KEY AUTOINCREMENT, card_num TEXT, month TEXT, year TEXT, cvv TEXT, Name on Card TEXT);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:986)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:593)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:590)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:61)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:33)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1850)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1773)
at com.antas.fingerprintauth.MyDatabaseHelper.onCreate(MyDatabaseHelper.java:34)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:412)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:317)
at com.antas.fingerprintauth.MyDatabaseHelper.addCard(MyDatabaseHelper.java:45)
at com.antas.fingerprintauth.AddDetails$1.onClick(AddDetails.java:60)
at android.view.View.performClick(View.java:7218)
at android.view.View.performClickInternal(View.java:7187)
at android.view.View.access$3500(View.java:817)
at android.view.View$PerformClick.run(View.java:27629)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7707)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:516)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:950)
А вот моя активность, из которой я вставляю данные в базу данных.
public class AddDetails extends AppCompatActivity {
EditText card_num, cvv, yr, mo, user;
int i = 0;
Button saveBtn;
ImageButton validate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_details);
card_num = (EditText) findViewById(R.id.cardnum);
cvv = (EditText) findViewById(R.id.cvv);
yr = (EditText) findViewById(R.id.year);
mo = (EditText) findViewById(R.id.month);
saveBtn = (Button) findViewById(R.id.save);
user = (EditText) findViewById(R.id.nameCard);
validate = (ImageButton) findViewById(R.id.visibility);
card_num.requestFocus();
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
.setError("Enter Name on Card");
}
else {*/
MyDatabaseHelper myDatabaseHelper = new MyDatabaseHelper(AddDetails.this);
myDatabaseHelper.addCard(card_num.getText().toString().trim(), mo.getText().toString().trim(), yr.getText().toString().trim(), cvv.getText().toString().trim(), user.getText().toString().trim());
}
});
}
}
Кто-нибудь, пожалуйста, помогите мне!