SQLite数据库没有创建
我在主类中使用了方法getwritabledatabase();
,但只显示了日志"构造函数被调用"然后申请被关闭。
这是我的代码
table extends SQLiteOpenHelper class
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.sql.SQLDataException;
import java.sql.SQLException;
public class Table extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Hammas's Database";
private static final String TABLE_NAME = "Hammas's Table";
private static final int DATABASE_VERSION = 1;
private static final String ID = "_id";
private static final String COLNAME = "Names";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COLNAME+" VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXIST "+TABLE_NAME+"";
private static final String TAG = "Hammas";
private Context context;
public Table(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.message(context,"Constructor is called");
Log.i(TAG,"CONSTRUCTOR IS CALLED");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
Message.message(context, "onCreate is called");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
Message.message(context,"onUpgrade is called");
}
}
消息类仅适用于吐司。
这是mainClass
enter code here
public class MainActivity extends ActionBarActivity {
Table table;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
table = new Table(this);
SQLiteDatabase sql= table.getReadableDatabase();
}
答案 0 :(得分:2)
此字符串无法按处理
private static final String TABLE_NAME = "Hammas's Table";
需要修改为
private static final String TABLE_NAME = "[Hammas''s Table]";
由于
[]
)。列名也是如此 要在字符串值中插入撇号,它也必须加倍。
答案 1 :(得分:1)
检查文件系统以确保:
//data/data/<Your-Application-Package-Name>/databases/Hammas's Database.db
已创建并位于此处。如果它已经存在,它将再次创建。