- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我试图创建 SQLiteDatabase 来保存我从 JSONObject 从发布请求中获取的数据。我的主要类(class):
public class MainActivity extends Activity {
static JSONObject result;
public Context mContext;
public SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
new UpdateData().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
private class UpdateData extends AsyncTask<String, Void, JSONObject>{
private JSONObject object;
@Override
protected JSONObject doInBackground(String... params) {
try {
HttpClient client = new DefaultHttpClient();
String postURL = "http://test.com";
HttpPost post = new HttpPost(postURL);
List<NameValuePair> crc = new ArrayList<NameValuePair>();
crc.add(new BasicNameValuePair("crc", "test"));
UrlEncodedFormEntity ent = new UrlEncodedFormEntity(crc,HTTP.UTF_8);
post.setEntity(ent);
HttpResponse responsePOST = client.execute(post);
HttpEntity entit = responsePOST.getEntity();
String retSrc = EntityUtils.toString(entit);
object = new JSONObject(retSrc);
} catch (Exception e) {
e.printStackTrace();
}
return object;
}
@Override
protected void onPostExecute(JSONObject result) {
MainActivity.this.result = result;
DataBase dbHelper = new DataBase(mContext) ;
db = dbHelper.getWritableDatabase();
dbHelper.createDB(db, result);
/*Cursor cursor = db.query("departments", null , null,
null, null, null, null, null);
System.out.println(cursor.getString(1));*/
}
}
}
和DataBaseHelper
public class DataBaseHelper extends SQLiteOpenHelper {
public boolean isDownloaded = false;
public boolean shutdown = false;
private Context mContext;
private JSONObject exams;
private JSONObject specs;
private JSONObject deps;
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "data.db";
private static final String EXAMS_TABLE_NAME = "exams";
private static final String EXAMS_TABLE_CREATE =
"CREATE TABLE " + EXAMS_TABLE_NAME + " (" +
"id VARCHAR (255), " + //0
"name VARCHAR (255)," + //1
"type VARCHAR (100)," + //2
"level VARCHAR (100)," + ");" ; //3
private static final String SPEC_TABLE_NAME = "specializations";
private static final String SPEC_TABLE_CREATE =
"CREATE TABLE " + SPEC_TABLE_NAME + " (" +
"id VARCHAR (255), " + //0
"name VARCHAR (255)," + //1
"name_en VARCHAR (255)," + //2
"description VARCHAR (255)," + ");" ; //3
private static final String DEP_TABLE_NAME = "departments";
private static final String DEP_TABLE_CREATE =
"CREATE TABLE " + DEP_TABLE_NAME + " (" +
"id VARCHAR (255), " + //0
"name VARCHAR (255)," + //1
"name_en VARCHAR (255)," + //2
"www VARCHAR (255)," + //3
"email VARCHAR (255)," + //4
"phone VARCHAR (100)," + ");" ; //5
public DataBase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
public boolean doesDBexist() {
File db;
db = new File("/data/data/my.package/databases/data.db");
return db.exists();
}
public boolean createDB(SQLiteDatabase db, JSONObject jsonObject) {
try { // if not working create new one
exams = jsonObject.getJSONObject("exams");
specs = jsonObject.getJSONObject("spec");
deps = jsonObject.getJSONObject("department");
db.execSQL(EXAMS_TABLE_CREATE); // here I get exception (described below)
db.execSQL(SPEC_TABLE_CREATE);
db.execSQL(DEP_TABLE_CREATE);
putContentValues(db);
isDownloaded = true;
return true;
} catch (SQLiteException e1) {
db = null;
return false;
} catch (Exception e3) {
shutdown = true;
return false;
}
}
public void putContentValues(SQLiteDatabase db) throws JSONException {
ContentValues cv = new ContentValues();
for (Iterator<String> it = exams.keys(); it.hasNext(); it.next()) {
JSONObject exam = exams.getJSONObject(it.toString());
cv.put("id", it.toString());
cv.put("name", exam.getString("name"));
cv.put("type", exam.getString("type"));
cv.put("level", exam.getString("level"));
db.insert(EXAMS_TABLE_NAME, null, cv);
cv.clear();
}
for (Iterator<String> it = specs.keys(); it.hasNext(); it.next()) {
JSONObject spec = specs.getJSONObject(it.toString());
cv.put("id", it.toString());
cv.put("name", spec.getString("name"));
cv.put("name_en", spec.getString("name_en"));
cv.put("desription", spec.getString("desription"));
db.insert(SPEC_TABLE_NAME, null, cv);
cv.clear();
}
for (Iterator<String> it = deps.keys(); it.hasNext(); it.next()) {
JSONObject dep = deps.getJSONObject(it.toString());
cv.put("id", it.toString());
cv.put("name", dep.getString("name"));
cv.put("name_en", dep.getString("name_en"));
cv.put("www", dep.getString("www"));
cv.put("email", dep.getString("email"));
cv.put("phone", dep.getString("phone"));
db.insert(DEP_TABLE_NAME, null, cv);
cv.clear();
}
}
}
我无法调试它。当我在 MainActivity
中创建 Cursor
时,我在行 db.execSQL(EXAMS_TABLE_CREATE);
android.database.sqlite.SQLiteException 之后得到异常: no such table: departments: ,编译时:SELECT * FROM departments
当我不创建游标时,它只是在调试时停在那里。我无法检查我的数据库中是否有任何内容以及它是否正确制作。我试图像有人在这个问题中说的那样重命名我的数据库: android.database.sqlite.SQLiteException: no such table但它没有解决这个问题。
最佳答案
这不可能是正确的:
"CREATE TABLE " + EXAMS_TABLE_NAME + " (" +
"id VARCHAR (255), " + //0
"name VARCHAR (255)," + //1
"type VARCHAR (100)," + //2
"level VARCHAR (100)," + ");" ;
删除该创建语句中的最后一个“,”,然后重试。
顺便说一句...您在所有创建语句中都犯了这个错误。
关于java - 不断收到 "SQLiteException: no such table"异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13724809/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!