我正在尝试创建一个与本机 android 和 html 共享数据库的应用程序。我初始化以下代码并为我的 webview 页面启用数据库。但是在 webview 页面上,当我更新数据库时,我没有得到这样的表(hybrid_user)。我不确定我在这里错过了什么。
数据库
public class DBHelper extends SQLiteOpenHelper{
private SQLiteDatabase helper;
private static final String DATABASE_NAME="hybrid_db";
private static final int SCHEMA_VERSION=1;
private static final String TAG = "DBHelper";
public DBHelper(Context context){
super(context, DATABASE_NAME, null, SCHEMA_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Log.i(TAG, "DBHelper");
db.execSQL("CREATE TABLE hybrid_user (" +
"tableid INTEGER PRIMARY KEY AUTOINCREMENT, " +
"userid TEXT," +
"password TEXT," +
"mobile TEXT," +
"createdate TIMESTAMP default current_timestamp);");
}
活动
webView = (WebView)findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/www/index.html");
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDatabasePath("/data/data/com.example.hybrid/hybrid_db");
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url){
webView.loadUrl("javascript:callFromActivity(\""+userid+"\")");
}
});
webView.setWebChromeClient(new WebChromeClient(){
public void onExceededDatabaseQuota(String url, String databaseIdentifier,
long currentQuota, long estimatedSize, long totalUsedQuota,
WebStorage.QuotaUpdater quotaUpdater){
quotaUpdater.updateQuota(204801);
}
});
索引.html
<script>
var hybrid_user = null;
function errorHandler(transaction, error){
alert('Error:'+error.message+' (Code '+error.code+')');
return true;
}
window.onerror = errorHandler;
try{
if(window.openDatabase){
var shortName = 'hybrid_db';
var version = '1.0';
var displayName = 'Hybrid Database';
var maxSize = 204801;
hybrid_user = openDatabase(shortName, version, displayName, maxSize);
alert("alert: "+hybrid_user);
}
else{
alert("Database is not supported");
}
}
catch(e){
if(e == 2){
alert("Invalid database version.");
}
else{
alert("Unknown error "+e+".");
}
return;
}
function updateRecord(){
hybrid_user.transaction(
function(transaction){
transaction.executeSql("UPDATE hybrid_user SET mobile=? WHERE userid ='"+document.getElementById("userid")+"'", [mobileNum]);
alert("mobileNum:" +mobileNum);
alert("userid:" +document.getElementById("userid"));
});
}
</script>
<body>
<form>
<fieldset>
<legend>Hybrid Application</legend>
<p>
<label>UserName: </label> <p id="userid"></p>
<p>
<label>Mobile Number :</label>
<input type = "text"
id = "mobileNum" />
</p>
<button type="button"
onclick="updateRecord()">Submit</button>
<div id="output"></div>
</fieldset>
</form>
</body>
我alert:[object Database]
从alert("alert:" +hybrid_user);
. 但是我仍然收到 sqlite 错误,没有这样的表。sqlite returned: error code = 1, msg = no such table:hybrid_user