Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

The following error occurs when I click on insert Button

*Error : android.database.sqlite.SQLiteException; near "student_name" ; syntax error (code 1) ; while compiling : CREATE TABLE IF NOT EXISTS studentTable ( _id TEXT PRIMARY KEY student_name TEXT NOT NULL student_rollno TEXT NOT NULL );*

Coding

DbActivity.java

package com.example.a;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;


public class DbActivity  {
public static final String KEY_ID = "_id";
public static final String KEY_NAME = "student_name";
public static final String KEY_ROLLNO = "student_rollno";


private static final String DATABASE_NAME= "Studentdb";
private static final int DATABASE_VERSION  = 1;
private static final String  DATABASE_TABLE = "studentTable";

private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;

class DbHelper extends SQLiteOpenHelper {

    public DbHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {

        // TODO Auto-generated method stub
        db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " ( " + 
        KEY_ID + " TEXT PRIMARY KEY " +
        KEY_NAME + " TEXT NOT NULL , " + 
        KEY_ROLLNO + " TEXT NOT NULL );" 
        );
            }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
        onCreate(db);
    }

}


public DbActivity(Context c) {
ourContext = c;
}

public DbActivity open() throws SQLException{
    ourHelper = new DbHelper(ourContext);
    ourDatabase = ourHelper.getWritableDatabase();
    return this;
}

public void close() {
    ourHelper.close();
}

public void createEntry(String name, String id, String rollno) {
    // TODO Auto-generated method stub
    ContentValues cv = new ContentValues();
    cv.put(KEY_ID, id);
    cv.put(KEY_NAME, name);
    cv.put(KEY_ROLLNO, rollno);
    ourDatabase.insert(DATABASE_TABLE, null, cv);
}

MainActivity.java

package com.example.a;

import com.example.a.DbActivity;
import com.example.a.R;
import com.example.a.NextActivity;
import com.example.a.MainActivity;

import android.os.Bundle;
import android.app.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

Button sqlUpdate,sqlView,sqlInsert,sqlDelete,sqlNext,sqlBack;
  EditText sqlName,sqlRollno,sqlId;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    sqlDelete = (Button) findViewById(R.id.bSQLDelete);
    sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
    sqlView = (Button) findViewById(R.id.bSQLView);
    sqlInsert = (Button) findViewById(R.id.bSQLInsert);
    sqlNext = (Button) findViewById(R.id.bSQLNext);
    sqlBack = (Button) findViewById(R.id.bSQLBack);

    sqlName = (EditText) findViewById(R.id.etSQLName);
    sqlRollno = (EditText) findViewById(R.id.etSQLRollno);
    sqlId = (EditText) findViewById(R.id.etSQLId);



    sqlUpdate.setOnClickListener(this);
    sqlView.setOnClickListener(this);
    sqlInsert.setOnClickListener(this);
    sqlNext.setOnClickListener(this);
    sqlDelete.setOnClickListener(this);
    sqlBack.setOnClickListener(this);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public void onClick(View v) {
    switch (v.getId()) {

    case R.id.bSQLNext :

         Intent i = new Intent(this,NextActivity.class);
         startActivity(i);           
         break;

    case R.id.bSQLInsert :

        boolean diditwork = true;        
         try {
            String name = sqlName.getText().toString();
            String rollno = sqlRollno.getText().toString();
        //    long lrollno = Long.parseLong(rollno);

            String id = sqlRollno.getText().toString();
          //  long lid = Long.parseLong(id);


             DbActivity empty = new DbActivity(MainActivity.this);
             empty.open();
             empty.createEntry(id,name,rollno);
             empty.close();
        } catch (Exception e) {
            diditwork = false;
            Dialog d = new Dialog(this);
            String error = e.toString();
            d.setTitle("dang");
            TextView tv = new TextView(this);
            tv.setText(error);
            d.setContentView(tv);
            d.show();
        }finally {
            if(diditwork)
            {
            Dialog d = new Dialog(this);
            d.setTitle("hech ya");
            TextView tv = new TextView(this);
            tv.setText("success");
            d.setContentView(tv);
            d.show();
        }

    }
share|improve this question
1  
You missed a comma on TEXT PRIMARY KEY. Should be : TEXT PRIMARY KEY, –  Aprian Jul 16 '13 at 7:22
add comment

2 Answers

up vote 2 down vote accepted

Your CREATE statement is missing a comma after TEXT PRIMARY KEY

db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " ( " + 
KEY_ID + " TEXT PRIMARY KEY , " +
KEY_NAME + " TEXT NOT NULL , " + 
KEY_ROLLNO + " TEXT NOT NULL );" 
);
share|improve this answer
add comment

You are missing a comma , after the primary key

Just add a comma after PRIMARY KEY

db.execSQL("CREATE TABLE IF NOT EXISTS " + DATABASE_TABLE + " ( " + 
        KEY_ID + " TEXT PRIMARY KEY, " +
        KEY_NAME + " TEXT NOT NULL , " + 
        KEY_ROLLNO + " TEXT NOT NULL );" 
        );
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.