单击插入按钮
时发生以下错误* 错误:android.database.sqlite.SQLiteException;靠近“student_name”;语法错误(代码1);编译时:CREATE TABLE IF NOT EXISTS studentTable(_id TEXT PRIMARY KEY student_name TEXT NOT NULL student_rollno TEXT NOT NULL); *
编码
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();
}
}
答案 0 :(得分:7)
您的CREATE
语句在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 );"
);
答案 1 :(得分:1)
您在,
primary key
只需在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 );"
);
答案 2 :(得分:0)
尝试在create table命令中删除“IF NOT EXIST”,android版本4.0.4中的SQLite给出此错误,而不是4.4.2
运气。