我试图使用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数据库类工作
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;
}
}
答案 0 :(得分:0)
您无法使用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);