您好我在Android中创建了一个SQLite数据库,其中我最初插入了两个记录,但是在执行数据时没有插入,数据库显示为空。任何人都可以帮我插入数据
在下面提供我的数据库类
public class DummyDatabase extends SQLiteOpenHelper{
public static final int NAME_COLUMN=2;
static final String DATABASE_NAME = "NestDatabase.db";
static final int DATABASE_VERSION = 1;
static final String tableName="Employees";
static final String DATABASE_CREATE = "create table "
+ " Employees "+ " " + " "
+ " ( "
+ " ID "
+ " integer primary key autoincrement , "
+ " NAME text ,
EMPLOYEE_CODE text,"
+ " MOBILE_NUMBER integer ); ";
public SQLiteDatabase db;
private final Context context;
public DataBaseHelper1 dbHelper;
public DummyDatabase(Context _context) {
super(_context,DATABASE_NAME, null,DATABASE_VERSION);
// TODO Auto-generated constructor stub
context=_context;
dbHelper=new DataBaseHelper1(_context,DATABASE_NAME,
null,DATABASE_VERSION);
}
public DummyDatabase open() throws SQLException
{
db=dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public int deleteEntry(String NAME)
{
String where="NAME=?";
int numberOFEntriesDeleted= db.delete("Employees", where, new String[]
{NAME}) ;
Toast.makeText(context, "Number fo Entry Deleted Successfully :
"+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getEntry(String Emp_code)
{
Cursor cursor=db.query("Employees", null, " EMPLOYEE_CODE=?", new
String[]{Emp_code}, null, null, null);
if(cursor.getCount()<1) // UserName Not Exist
return "NOT EXIST";
cursor.moveToFirst();
String user= cursor.getString(cursor.getColumnIndex("NAME"));
return user;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE);
db.execSQL("insert into Employees( NAME , EMPLOYEE_CODE , MOBILE_NUMBER)
"+" values ('Alexander','A111','1234567890');");
db.execSQL("insert into Employees( NAME , EMPLOYEE_CODE , MOBILE_NUMBER)
"+" values ('Bernie','B111','1234567890');");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w("TaskDBAdapter","Upgrading from
version"+oldVersion+"to"+newVersion+",which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS"+"TEMPLATE");
onCreate(db);
}
}
答案 0 :(得分:2)
您必须在SQLiteOpenHelper的onCreate
方法中创建数据表。
CREATE TABLE IF NOT NOT DISABASE_CREATE;
将插入语句更改为
db.execSQL("insert into Employees( NAME , EMPLOYEE_CODE , MOBILE_NUMBER)
VALUES ('Alexander','A111','1234567890');");
答案 1 :(得分:1)
首先在onCreate中你必须创建这样的表:
db.execSQL("CREATE TABLE "+DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + " TEXT NOT NULL);"
);
编辑:
public long createEntry(String name, String employeeCode, int mobileNum) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(NAME, name);
cv.put(EMPLOYEE_CODE, employeeCode);
cv.put(MOBILE_NUMBER, mobileNum);
return db.insert("Employees", null, cv);
}
从oncreate调用此方法并传递要插入的内容。
答案 2 :(得分:1)
onCreate
。所以我的猜测是,数据库存在(来自早期的代码运行,可能没有此时的插入),而onCreate
根本就没有被调用。只需在Log.d()
中添加onCreate
即可进行检查。另外,仅定义create table
是不够的,还需要在onCreate()
中对数据库执行它: - 。
更进一步,您应该在代码中更改一些内容。其中一个也可以立即解决你的问题。
create table
并且应在onCreate
和onUpgrade
中对表格结构(新表格,列,观点)进行更改。这是结构更改和插入初始数据的正确位置。onCreate
应该只包含onUpgrade(db, 0, 1)
行,并且您在onUpgrade
中执行所有操作。commit
您的工作。在此之前,数据不会写入磁盘,在下一次运行中,您无法再选择它。db.insert
时使用db.execSQL
。它意味着Java程序员更容易使用。onCreate
仅在您第一次创建数据库时调用,然后再调用!在开发过程中,这可能很烦人,因为您必须在每次使用context.deleteDatabase(DATABASE_NAME)
的测试运行之前删除整个数据库。答案 3 :(得分:0)
签出创建表查询。我给你一个样本查询。
CREATE TABLE IF NOT EXISTS menus (menuname TEXT,screens TEXT,menuitem TEXT,menuposition TEXT,menuid TEXT,PRIMARY KEY(menuid));
并在插入之前执行create table query。在onCreate()
方法中添加以上代码。
答案 4 :(得分:0)
你忘了创建TABLE。您需要在OnCreate中执行DATABASE_CREATE查询或在onUpgrade表中执行(可选)UPDATE表。请参阅以下代码,请参阅UPDATED和ADDED标签
.. SQLite说,主键必须命名为_id
public class DummyDatabase extends SQLiteOpenHelper {
public static final int NAME_COLUMN = 2;
static final String DATABASE_NAME = "NestDatabase.db";
static final int DATABASE_VERSION = 1;
static final String tableName = "Employees";
static final String DATABASE_CREATE = "CREATE TABLE employees (_id INTEGER PRIMARY KEY, name TEXT, mobile_number TEXT);"; /** UPDATED **/
public SQLiteDatabase db;
private final Context context;
public DataBaseHelper1 dbHelper;
public DummyDatabase(Context _context) {
super(_context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
context = _context;
dbHelper = new DataBaseHelper1(_context, DATABASE_NAME,
null, DATABASE_VERSION);
}
public DummyDatabase open() throws SQLException {
db = dbHelper.getWritableDatabase();
return this;
}
public void close() {
db.close();
}
public int deleteEntry(String NAME) {
String where = "NAME=?";
int numberOFEntriesDeleted = db.delete("Employees", where, new String[]
{NAME});
Toast.makeText(context, "Number fo Entry Deleted Successfully :
"+numberOFEntriesDeleted, Toast.LENGTH_LONG).show();
return numberOFEntriesDeleted;
}
public String getEntry(String Emp_code) {
Cursor cursor = db.query("Employees", null, " EMPLOYEE_CODE=?", new
String[]{Emp_code}, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
return "NOT EXIST";
}
cursor.moveToFirst();
String user = cursor.getString(cursor.getColumnIndex("NAME"));
return user;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(DATABASE_CREATE); //** ADDED */
db.execSQL("insert into Employees( NAME , EMPLOYEE_CODE , MOBILE_NUMBER)
"+"values('Alexander', 'A111', '1234567890'); ");
db.execSQL("insert into Employees( NAME , EMPLOYEE_CODE , MOBILE_NUMBER)
"+"values('Bernie', 'B111', '1234567890'); ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
答案 5 :(得分:0)
看看这个例子
public class RouteDbAdapter
{
public static final String KEY_PLACE = "place";
public static final String KEY_LATITUDE = "latitude";
public static final String KEY_LONGITUDE = "longitude";
public static final String KEY_ROWID = "_id";
private static final String TAG = "RouteDbAdapter";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table routes (_id integer primary key autoincrement, "
+ "place text not null, latitude real not null, longitude real not null);";
private static final String DATABASE_NAME = "cojDB";
private static final String DATABASE_TABLE = "routes";
private static final int DATABASE_VERSION = 2;
private final Context mCtx;
private static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS routes");
onCreate(db);
}
}
public RouteDbAdapter(Context ctx)
{
this.mCtx = ctx;
}
public RouteDbAdapter open() throws SQLException
{
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close()
{
mDbHelper.close();
}
public long createPlace(String place, Double lat,Double lng)
{
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PLACE, place);
initialValues.put(KEY_LATITUDE, lat);
initialValues.put(KEY_LONGITUDE, lng);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deletePlace(String place)
{
return mDb.delete(DATABASE_TABLE, KEY_PLACE + "='" + place+"'", null) > 0;
}
public Cursor fetchAllPlace()
{
return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_LATITUDE,
KEY_LONGITUDE,KEY_PLACE}, null, null, null, null, null);
}
public Cursor fetchPlace(String place) throws SQLException
{
Cursor mCursor =
mDb.query(true, DATABASE_TABLE, new String[] {KEY_LATITUDE,KEY_LONGITUDE}, KEY_PLACE + "= '" + place+"'", null,
null, null, null, null);
if (mCursor != null)
{
mCursor.moveToFirst();
}
return mCursor;
}
}