Java 数据未插入android上的SQLite数据库?
我开发了一个Android数据库应用程序。Java 数据未插入android上的SQLite数据库?,java,android,database,sqlite,Java,Android,Database,Sqlite,我开发了一个Android数据库应用程序。 问题是,当我单击“添加”按钮时,一条消息显示数据已成功添加。 但数据不会插入数据库。 然后我用SQLite查看器打开数据库,在那里我找不到我插入的任何数据 注册页面代码: package com.example.dbapp; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; imp
问题是,当我单击“添加”按钮时,一条消息显示数据已成功添加。
但数据不会插入数据库。
然后我用SQLite查看器打开数据库,在那里我找不到我插入的任何数据 注册页面代码:
package com.example.dbapp;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class Register extends Activity {
Button add;
TextView b,c;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
add = (Button) findViewById(R.id.btnReg);
b = (TextView) findViewById(R.id.etName);
c = (TextView) findViewById(R.id.etOccu);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
try {
String name = b.getText().toString();
String occ = c.getText().toString();
DataCon entry = new DataCon(Register.this);
entry.open();
entry.createEntry(name, occ);
entry.close();
Toast toast = Toast.makeText(getApplicationContext(), "Sucess", Toast.LENGTH_SHORT);
toast.show();
}
catch(Exception e) {
String err = e.toString();
Toast toast = Toast.makeText(getApplicationContext(), err, Toast.LENGTH_SHORT);
toast.show();
}
}
});
}
数据库连接代码:
package com.example.dbapp;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
public class DataCon {
public static final String rowid = "_id";
public static final String name = "persons_name";
public static final String hot = "persons_hotness";
private static final String DB_NAME = "myDB";
private static final String DB_TABLE = "tbl_me";
private static final int DB_VER = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context) {
super(context, DB_NAME, null, DB_VER);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + name + "TEXT NOT NULL, " +
hot + " TEXT NOT NULL);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +DB_TABLE);
onCreate(db);
}
}
public DataCon(Context c) {
ourContext = c;
}
public DataCon open() {
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close() {
ourHelper.close();
}
public long createEntry(String name2, String occ) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(name, name2);
cv.put(hot, occ);
return ourDatabase.insert(DB_TABLE, null, cv);
}
}
现在我该怎么解决这个问题呢?感谢您的sql中出现错误。在列名和类型之间没有提供空格。正确的应该是
db.execSQL("CREATE TABLE " + DB_TABLE + " (" + rowid +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + name + " TEXT NOT NULL, " +
--------------------------- ^
hot + " TEXT NOT NULL);");`
LogCat中有错误吗?尝试从应用程序中读取相同的数据。如果有错误,请发布日志猫?没有,没有日志猫错误。我也尝试从我的应用程序读取数据,但失败…:(你收到任何错误吗?你看到创建的表了吗?删除你的db和tryNo没有错误。我看到使用sqlite viewer创建的表…但是没有数据删除表并运行它的工作…但是有什么问题吗?我删除数据库并运行插入的应用程序和数据…许多THNK…之后你更改了代码。)因此,在数据库创建之后,新代码包含无效的sql。这就是它无法工作的原因