-3

This is my code:

    package com.example.user.sqliteapp2;

    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;

    public class MainActivity extends AppCompatActivity {

    DatabaseHelper myDb;
    EditText editName,editSurname,editMarks;
    Button btnAddData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    myDb = new DatabaseHelper(this);

    editName=(EditText)findViewById(R.id.editText_name);
    editSurname=(EditText)findViewById(R.id.editText_surname);
    editMarks=(EditText)findViewById(R.id.editText_marks);
    btnAddData=(Button) findViewById(R.id.button_add);
    AddData();
    }
    public void AddData(){
    btnAddData.setOnClickListener(
    new View.OnClickListener(){
    @Override
    public void onClick(View v) {
    Boolean inserted= myDb.insertData(editName.getText().toString(),
    editSurname.getText().toString(),
    editMarks.getText().toString());
    if (inserted==true )
    Toast.makeText(MainActivity.this,"DATAINSERTed",
    Toast.LENGTH_LONG).show();
    else
    Toast.makeText(MainActivity.this,"DATAINSERTed",
    Toast.LENGTH_LONG).show();
    }
    }
    );
    } 

After compiling the app, I click on an add data button to insert some data. Then it show this fatal error:

    '[ 10-24 10:38:38.886  2439: 2439 D/         ]
    HostConnection::get() New Host Connection established 0x7fc5fe207940,        tid 2439
    I/OpenGLRenderer: Initialized EGL, version 1.4
    I/Choreographer: Skipped 42 frames!  The application may be doing too        much work on its main thread.
    E/SQLiteLog: (1) near "table": syntax error
    D/AndroidRuntime: Shutting down VM

    --------- beginning of crash
    E/AndroidRuntime: FATAL EXCEPTION: main

    Process: com.example.user.sqliteapp2, PID: 2439

    android.database.sqlite.SQLiteException: near "table": syntax error(code 1):  -while compiling: create table student.table ( ID INTEGER PRIMARY KEY                -#-AUTOINCREMENT,NAME TEXT,SURNAME TEXT,MARKS INTEGER )at -       android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method at         android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnect#        ion.java:887) 
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:498) 
    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:1674)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1605)
    at com.example.user.sqliteapp2.DatabaseHelper.onCreate(DatabaseHelper.java:26)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.jav#a:251)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.j#ava:163) 
    at com.example.user.sqliteapp2.DatabaseHelper.insertData(DatabaseHelper.java:36)
    at com.example.user.sqliteapp2.MainActivity$1.onClick(MainActivity.java:32)
    at android.view.View.performClick(View.java:5198)
    at android.view.View$PerformClick.run(View.java:21147)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:148) at         android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)'

How can I fix this problem?

SiHa
  • 7,830
  • 13
  • 34
  • 43
Jun Jie
  • 13
  • 1
  • 3
  • 8

1 Answers1

0

Problem is in your DatabaseHelper, you need to name your table properly. Table name cannot contain ..

Check your logcat output android.database.sqlite.SQLiteException: near "table": syntax error(code 1): -while compiling: create table student.table it says you are attempting to create a table with name student.table which is invalid.

check this thread for more details.

Community
  • 1
  • 1
Nayan Srivastava
  • 3,655
  • 3
  • 27
  • 49
  • but i still cannot found what wrong on the student.table . even i delete the '.' – Jun Jie Oct 25 '16 at 02:28
  • What is logcat saying now? – Nayan Srivastava Oct 25 '16 at 04:30
  • '.' pretty much signifies table inside a db – Nayan Srivastava Oct 25 '16 at 04:33
  • still with same error , after i replace the 'TABLE_NAME="student.table"' to "TABLE_NAME="student"' in table name. – Jun Jie Oct 25 '16 at 05:22
  • Boss what logcat is saying now? – Nayan Srivastava Oct 25 '16 at 05:39
  • 1:50:57 PM Executing tasks: [:app:clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:assembleDebug] 1:51:44 PM Gradle build finished in 46s 647ms 1:52:11 PM Instant Run re-installed and restarted the app – Jun Jie Oct 25 '16 at 05:52
  • thisnis the event log – Jun Jie Oct 25 '16 at 05:53
  • 10-25 05:53:37.159 1585-2631/system_process E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f3a60a0e760 – Jun Jie Oct 25 '16 at 06:11
  • 10-25 05:53:37.195 1914-2511/com.android.inputmethod.latin E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f3a6661b270 10-25 05:53:37.380 1233-1649/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000 10-25 05:54:02.780 1935-1935/com.google.android.gms.persistent E/NetworkScheduler.ATC: Provided calling package not found: com.google.android.apps.photos this is the logcat – Jun Jie Oct 25 '16 at 06:12