gpt4 book ai didi

android - 错误 : android. database.sqlite.SQLiteException;在 "student_name"附近;语法错误(代码 1);编译时:CR

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:21:39 24 4
gpt4 key购买 nike

点击插入按钮出现如下错误

*错误: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);
}

主 Activity .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();
}

}

最佳答案

您的 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 );"
);

关于android - 错误 : android. database.sqlite.SQLiteException;在 "student_name"附近;语法错误(代码 1);编译时:CR,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17670377/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com