smartF smartF - 1 month ago 11
Java Question

E/AndroidRuntime: FATAL EXCEPTION: main (main Register issue)

While Testing my app i got this error. And i am unable to resolve this as i am newbee in Java

E/SQLiteLog: (1) near "tableRegister": syntax error
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main

Process: tweeteve1.loginregister, PID: 3348
android.database.sqlite.SQLiteException: near "tableRegister": syntax error (code 1): , while compiling: create tableRegister(user_id integer primary key autoincrement, Name text, UserName text, Password text, Age int)
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.executeSql(SQLiteDatabase.java:1675)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1606)
at tweeteve1.loginregister.DatabaseHelper.onCreate(DatabaseHelper.java:28)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:251)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at tweeteve1.loginregister.DatabaseHelper.insertData(DatabaseHelper.java:37)
at tweeteve1.loginregister.Register$1.onClick(Register.java:37)
at android.view.View.performClick(View.java:5610)
at android.view.View$PerformClick.run(View.java:22260)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Application terminated.


my code is here
Register file:

package tweeteve1.loginregister;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.util.jar.Attributes;

public class Register extends AppCompatActivity {
DatabaseHelper myDB;
EditText ePassword;
EditText eUserName;
EditText eName;
EditText eAge;
Button bRegister;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
myDB = new DatabaseHelper(this);
eAge = (EditText) findViewById(R.id.etAge);
eName = (EditText) findViewById(R.id.etName);
eUserName = (EditText) findViewById(R.id.etUserName);
ePassword = (EditText) findViewById(R.id.etPassword);
bRegister = (Button) findViewById(R.id.btRegister);
AddData();

}
public void AddData() {
bRegister.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean isInserted = myDB.insertData(eName.getText().toString(),
eUserName.getText().toString(),
ePassword.getText().toString(),
eAge.getText().toString()
);
if(isInserted == true)
Toast.makeText(Register.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(Register.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
}


Do help me please in solving my problem Thank you

Answer

The reason is actually self evident - right in the error message - look at your SQL statement that's being executed:

create tableRegister(user_id integer primary key autoincrement, Name text, UserName text, Password text, Age int)

As you can see, there's a space missing between table and Register. The stack trace is also very helpfully points out that the error is in line 28 of DatabaseHelper.java file:

at tweeteve1.loginregister.DatabaseHelper.onCreate(DatabaseHelper.java:28)

So, look at DatabaseHelper.java class, line 28, and add the missing space.

EDIT: based on the create table code from the comment, change it to be:

db.execSQL("create table " + TableName + " (user_id integer primary key autoincrement, Name text, UserName text, Password text, Age int)");

Note the spaces after table and before (user_id.

Comments