gpt4 book ai didi

android - 故障1( "table": syntax error) on 0x39c298 when preparing 'CREATE TABLE table (附近

转载 作者:行者123 更新时间:2023-11-29 23:28:10 30 4
gpt4 key购买 nike

我正在尝试使用包含 200 名学生的数据库来执行应用程序,但我不知道数据库创建时出现的问题。请帮忙。另外,如果有人能告诉我如何初始化两列并让其余列保持为空,这也会很有帮助。

public class Work {

public static final String KEY_NAME = "name";
public static final String KEY_REG = "reg";
public static final String KEY_QUIZ1 = "q1";
public static final String KEY_QUIZ2 = "q2";
public static final String KEY_QUIZ3 = "q3";
public static final String KEY_CAT1 = "c1";
public static final String KEY_CAT2 = "c2";
public static final String KEY_TEE = "t";

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

private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "table";
private static final int DATABASE_VERSION = 1;

private static 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 " + DATABASE_TABLE + " (" +
KEY_REG + " TEXT, " +
KEY_NAME + " TEXT, " +
KEY_QUIZ1 + " TEXT, " +
KEY_QUIZ2 + " TEXT, " +
KEY_QUIZ3 + " TEXT, " +
KEY_CAT1 + " TEXT, " +
KEY_CAT2 + " TEXT, " +
KEY_TEE + " TEXT);"

);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TBALE IF EXIISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public Work(Context c){
ourContext = c;

}

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

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

public long createEntry(String string, String string2) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_REG, string);
cv.put(KEY_NAME, string2);
/* cv.put(KEY_QUIZ1, string3);
cv.put(KEY_QUIZ2, string4);
cv.put(KEY_QUIZ3, string5);
cv.put(KEY_CAT1, string6);
cv.put(KEY_CAT2, string7);
cv.put(KEY_TEE, string8);*/
return ourDatabase.insert(DATABASE_TABLE, null, cv);

}


}

有人可以帮我找出错误吗?我对 SQL 还很陌生,而且对它的方式有点冷淡。

这是我用来在 SQLDatabase 中创建 200 个条目的代码

        case R.id.bres:
int i;
boolean did=true;
try{
Work e = new Work(this);
e.open();
for(i=0;i<200;i++)
{
e.createEntry(reg2[i],na[i],"0","0","0","0","0","0");
}
e.close();
}
catch(Exception e){
did=false;
Dialog d = new Dialog(this);
d.setTitle("Failed");
TextView tv= new TextView(this);
tv.setText("Error");
d.setContentView(tv);
d.show();
}
finally{
did=true;
}

这是我用来根据所选条目显示数据的代码

        Work z = new Work(this);
z.open();
String qu1 ="QUIZ 1\n" + z.getq1(j);
String qu2 ="QUIZ 2\n" +z.getq2(j);
String qu3 = "QUIZ 3\n"+z.getq3(j);
String ca1 ="CAT 1\n"+ z.getc1(j);
String ca2 ="CAT 2\n"+ z.getc2(j);
String te ="TERM END\n"+ z.gette(j);
z.close();
a.setText(qu1);
b.setText(qu2);
c.setText(qu3);
d.setText(ca1);
e.setText(ca2);
f.setText(te);

这是 SQL 数据库类 Work

public class Work {

public static final String KEY_NAME = "name";
public static final String KEY_REG = "reg";
public static final String KEY_QUIZ1 = "q1";
public static final String KEY_QUIZ2 = "q2";
public static final String KEY_QUIZ3 = "q3";
public static final String KEY_CAT1 = "c1";
public static final String KEY_CAT2 = "c2";
public static final String KEY_TEE = "t";

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

private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "stable";
private static final int DATABASE_VERSION = 1;

private static 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 " + DATABASE_TABLE + " (" +
KEY_REG + " TEXT, " +
KEY_NAME + " TEXT, " +
KEY_QUIZ1 + " TEXT, " +
KEY_QUIZ2 + " TEXT, " +
KEY_QUIZ3 + " TEXT, " +
KEY_CAT1 + " TEXT, " +
KEY_CAT2 + " TEXT, " +
KEY_TEE + " TEXT);"

);
}

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

}

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

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

public long createEntry(String string, String string2, String string3, String string4, String string5, String string6, String string7, String string8) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_REG, string);
cv.put(KEY_NAME, string2);
cv.put(KEY_QUIZ1, string3);
cv.put(KEY_QUIZ2, string4);
cv.put(KEY_QUIZ3, string5);
cv.put(KEY_CAT1, string6);
cv.put(KEY_CAT2, string7);
cv.put(KEY_TEE, string8);
return ourDatabase.insert(DATABASE_TABLE, null, cv);

}

public String getq1(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_QUIZ1);
for(c.moveToFirst();i<j;i++,c.moveToNext())
{}
res = c.getString(iQ1);
return res;
}

public String getq2(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";

int iQ1=c.getColumnIndex(KEY_QUIZ2);
for(c.moveToFirst();i<j;i++,c.moveToNext())
{}
res = c.getString(iQ1);
return null;
}

public String getq3(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";

int iQ1=c.getColumnIndex(KEY_QUIZ3);
for(c.moveToFirst();i<j;i++,c.moveToNext())
{}
res = c.getString(iQ1);
return null;
}

public String getc1(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{ KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";

int iQ1=c.getColumnIndex(KEY_CAT1);
for(c.moveToFirst();i<j;i++,c.moveToNext())
{}
res = c.getString(iQ1);
return null;
}

public String getc2(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{ KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";

int iQ1=c.getColumnIndex(KEY_CAT2);
for(c.moveToFirst();i<j;i++,c.moveToNext())
{}
res = c.getString(iQ1);
return null;
}

public String gette(int j) {
// TODO Auto-generated method stub
int i=0;
String[] columns = new String[]{KEY_REG, KEY_NAME, KEY_QUIZ1, KEY_QUIZ2, KEY_QUIZ3, KEY_CAT1, KEY_CAT2, KEY_TEE};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String res = "";
int iQ1=c.getColumnIndex(KEY_TEE);
for(c.moveToFirst();i<j;i++,c.moveToNext())
{}
res = c.getString(iQ1);
return null;
}
}

最佳答案

您不能使用reserved words 用于创建表。

另请参阅这篇文章Are there any reserved words in SQLite?

并且也改变

db.execSQL("DROP TBALE IF EXIISTS " + DATABASE_TABLE);

db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);

关于android - 故障1( "table": syntax error) on 0x39c298 when preparing 'CREATE TABLE table (附近,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26785493/

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