Android:SQLite数据库未创建
我使用以下代码创建表并将数据插入表中。仅当select语句位于同一文件中时,代码才会显示数据。我注意到/data/data中没有数据库。我是否遗漏了一些东西或过程实际上是错误的?如果错了,我该怎么办Android:SQLite数据库未创建,android,sqlite,Android,Sqlite,我使用以下代码创建表并将数据插入表中。仅当select语句位于同一文件中时,代码才会显示数据。我注意到/data/data中没有数据库。我是否遗漏了一些东西或过程实际上是错误的?如果错了,我该怎么办 surgicalmstDB = this.openOrCreateDatabase("surgicalmstDB ", SQLiteDatabase.CREATE_IF_NECESSARY, null); surgicalmstDB.setVersion(1); s
surgicalmstDB = this.openOrCreateDatabase("surgicalmstDB ", SQLiteDatabase.CREATE_IF_NECESSARY, null);
surgicalmstDB.setVersion(1);
surgicalmstDB.setLocale(Locale.getDefault());
surgicalmstDB.setLockingEnabled(true);
surgicalmstDB.execSQL("CREATE TABLE IF NOT EXISTS " +
" instrument " +
" (id int,instrumnt TEXT, category TEXT, details TEXT);");
surgicalmstDB.execSQL("INSERT INTO " +
"instrument" +
" Values (2,'#3 Long Knife Handle ','Cutting and dissecting','Used to cut deeper tissue.');");
//-----------------------------------------------------------------
String query="SELECT * from instrument WHERE id=2";
Cursor cursor=surgicalmstDB.rawQuery(query, null);
while (cursor.moveToNext()) {
String title =cursor.getString(cursor
.getColumnIndex("instrumnt"));
String author= cursor.getString(cursor
.getColumnIndex("category"));
String price= cursor.getString(cursor
.getColumnIndex("details"));
txt.setText(title+"-----"+author+"-------"+price);
}
cursor.close();
surgicalmstDB.close();
我建议您参考SQLiteOpenHelper,而不是硬编码查询 下面的代码将帮助您 员工模型类
package com.database;
public class Employee {
int _id;
String _name;
String _phone_number;
public Employee (){
}
// constructor
public Employee (int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
// constructor
public Employee (String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}
SQLite帮助程序类
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
公共类DatabaseHelper扩展了SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "employeeDB";
private static final String TABLE_EMPLOYEE = "employee";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public DatabaseHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_EMPLOYEE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
积垢操作
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
//添加新员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
//获得单身员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
//获取所有员工
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
//更新单个员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
//删除单个员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
插入新记录
public void addemployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
db.insert(TABLE_EMPLOYEE, null, values);
db.close();
}
public int updateEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
// updating row
return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
}
public void deleteEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
db.close();
}
正在阅读行
获取所有员工
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
更新记录
public void addemployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
db.insert(TABLE_EMPLOYEE, null, values);
db.close();
}
public int updateEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
// updating row
return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
}
public void deleteEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
db.close();
}
删除记录
public void addemployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
db.insert(TABLE_EMPLOYEE, null, values);
db.close();
}
public int updateEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
// updating row
return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
}
public void deleteEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
db.close();
}
我建议您参考SQLiteOpenHelper,而不是硬编码查询 下面的代码将帮助您 员工模型类
package com.database;
public class Employee {
int _id;
String _name;
String _phone_number;
public Employee (){
}
// constructor
public Employee (int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
// constructor
public Employee (String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}
SQLite帮助程序类
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
公共类DatabaseHelper扩展了SQLiteOpenHelper{
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "employeeDB";
private static final String TABLE_EMPLOYEE = "employee";
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public DatabaseHelper (Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_EMPLOYEE + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
积垢操作
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
//添加新员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
//获得单身员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
//获取所有员工
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
//更新单个员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
//删除单个员工
public void addEmployee(Employee employee) {}
public Contact getEmployee(int id) {}
public int updateEmployee(Employee employee) {}
public void deleteEmployee(Employee employee) {}
插入新记录
public void addemployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
db.insert(TABLE_EMPLOYEE, null, values);
db.close();
}
public int updateEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
// updating row
return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
}
public void deleteEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
db.close();
}
正在阅读行
获取所有员工
public List<Employee> getAllEmployees() {}
public Contact getEmployee(int id) {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.query(TABLE_EMPLOYEE, new String[] { KEY_ID,
KEY_NAME, KEY_PH_NO }, KEY_ID + "=?",
new String[] { String.valueOf(id) }, null, null, null, null);
if (cursor != null)
cursor.moveToFirst();
Employee employee= new Employee(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2));
return employee;
}
public List<Contact> getAllEmployees() {
List<Employee> employeeList = new ArrayList<Employee>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_EMPLOYEE;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Employee employee= new Employee();
employee.setID(Integer.parseInt(cursor.getString(0)));
employee.setName(cursor.getString(1));
employee.setPhoneNumber(cursor.getString(2));
employeeList.add(employee);
} while (cursor.moveToNext());
}
return employeeList;
}
更新记录
public void addemployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
db.insert(TABLE_EMPLOYEE, null, values);
db.close();
}
public int updateEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
// updating row
return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
}
public void deleteEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
db.close();
}
删除记录
public void addemployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
db.insert(TABLE_EMPLOYEE, null, values);
db.close();
}
public int updateEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, employee.getName());
values.put(KEY_PH_NO, employee.getPhoneNumber());
// updating row
return db.update(TABLE_EMPLOYEE, values, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
}
public void deleteEmployee(Employee employee) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_EMPLOYEE, KEY_ID + " = ?",
new String[] { String.valueOf(employee.getID()) });
db.close();
}
因此,当您检查/data/data/dir时,您可能已经关闭了仿真器,我建议您打开仿真器,然后检查它。因此,当您检查/data/data/dir时,您可能已经关闭了仿真器,我建议您打开仿真器,然后检查它。