刚刚开始Android,我正在尝试创建一个简单的应用程序,它有一个用于收集名称和联系人的SQLite数据库。我创建了一个视图活动,它将使用simpleCursorAdapter在列表视图中显示内容(是的,它已经折旧,但我还没有使用自定义适配器。无论如何,到目前为止,代码编译得很好。当我点击按钮开始时视图活动我得到了
SQLite异常:没有这样的表:clientTable(代码1):,编译时:SELECT _id,name,mobile FROM clientTable。
下面是我的数据库类的代码
package com.example.ideahutquizz;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class ClientDatabase extends SQLiteOpenHelper {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "name";
public static final String KEY_MOBILE = "mobile";
public static final String DATABASE_NAME = "clientDatabase";
public static final String TABLE_NAME = "clientTable";
public static final int DATABASE_VERSION = 1;
String CREATE_TABLE = "create table"
+ TABLE_NAME + "("
+ KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_NAME + "TEXT NOT NULL,"
+ KEY_MOBILE + "TEXT NOT NULL" + ")";
public ClientDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
以下是视图活动的代码
package com.example.ideahutquizz;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
public class ViewDataActivity extends ActionBarActivity {
Button back;
ListView clientList;
SQLController clientDB;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_data);
clientList = (ListView)findViewById(R.id.listView1);
back = (Button)findViewById(R.id.button1);
clientDB = new SQLController(this);
clientDB.open();
Cursor cursor = clientDB.readData();
String[] from = new String[]{ClientDatabase.KEY_ROWID, ClientDatabase.KEY_NAME, ClientDatabase.KEY_MOBILE};
int[] to = new int[]{R.id.textView_id, R.id.textView_name, R.id.textView_mobile};
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listview_item_row, cursor, from, to);
adapter.notifyDataSetChanged();
clientList.setAdapter(adapter);
back.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
clientDB.close();
Intent back = new Intent(ViewDataActivity.this, ControlPanel.class);
startActivity(back);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.view_data, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
非常感谢任何帮助。
...更新
我按如下方式更改了create table语句,但我仍然遇到同样的错误......
private static final String CREATE_TABLE =
"create table " + TABLE_NAME + "("
+ KEY_ROWID + " integer primary key autoincrement,"
+ KEY_NAME + " text not null,"
+ KEY_MOBILE + " text not null" + ")";
答案 0 :(得分:1)
未创建表:
String CREATE_TABLE = "create table"
+ TABLE_NAME + "("
会产生这个字符串:
String CREATE_TABLE = "create tableclientTable("
您需要在create table之后添加一个空格:
String CREATE_TABLE = "create table "
+ TABLE_NAME + "("
字段名称
也是如此所以,最后,字符串将是:
String CREATE_TABLE = "CREATE TABLE " +
TABLE_NAME + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_MOBILE + " TEXT NOT NULL" + ")";
答案 1 :(得分:1)
由于缺少空格,您的CREATE TABLE语句将失败;
String CREATE_TABLE = "create table"
+ TABLE_NAME + "("
+ KEY_ROWID + "INTEGER PRIMARY KEY AUTOINCREMENT,"
+ KEY_NAME + "TEXT NOT NULL,"
+ KEY_MOBILE + "TEXT NOT NULL" + ")";
...会产生类似
的内容create tableclientTable(
_idINTEGER PRIMARY KEY AUTOINCREMENT,
nameTEXT NOT NULL,
mobileTEXT NOT NULL)
...这会导致语法错误而不会创建表。