java(android)在模拟器上运行,但不在实际设备上运行
这是一个android代码,该代码将在emulator上运行,但不会在实际设备上运行。基本上,它无法在实际设备上创建表,而这可以在android emulator上完成java(android)在模拟器上运行,但不在实际设备上运行,java,android,eclipse,windows,Java,Android,Eclipse,Windows,这是一个android代码,该代码将在emulator上运行,但不会在实际设备上运行。基本上,它无法在实际设备上创建表,而这可以在android emulator上完成 public class AddNewTransactionCategory extends ActionBarActivity { ListView listView; //ArrayList<String> categoryString; DataBaseTransactionAdapt
public class AddNewTransactionCategory extends ActionBarActivity {
ListView listView;
//ArrayList<String> categoryString;
DataBaseTransactionAdapter dbAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_new_transaction_category);
listView = (ListView) findViewById(R.id.categoryListView);
dbAdapter = new DataBaseTransactionAdapter(this);
dbAdapter.insertDataCategory("Shopping");
// TODO Create a Table for Category
Toast.makeText(this, dbAdapter.getAllDataCategory(), Toast.LENGTH_LONG).show();
//ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, categoryString);
//listView.setAdapter(adapter);
}
}
公共类AddNewTransactionCategory扩展了ActionBarActivity{
列表视图列表视图;
//阵列列表分类;
数据库事务适配器dbAdapter;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u add\u new\u transaction\u category);
listView=(listView)findViewById(R.id.categoryListView);
dbAdapter=新数据库TransactionAdapter(此);
dbAdapter.insertDataCategory(“购物”);
//TODO为类别创建表
Toast.makeText(this,dbAdapter.getAllDataCategory(),Toast.LENGTH_LONG.show();
//ArrayAdapter=新的ArrayAdapter(这是android.R.layout.simple\u list\u item\u 1,categoryString);
//setAdapter(适配器);
}
}
这是用于创建表的DataBaseTransactionAdapter
public class DataBaseTransactionAdapter {
DataBaseTransaction helper;
public DataBaseTransactionAdapter(Context context) {
helper = new DataBaseTransaction(context);
}
public long insertDataTransaction(int transaction) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseTransaction.TRANSACTION, transaction);
// id will be negative if something went wrong otherwise it contains row id
long id = db.insert(DataBaseTransaction.TABLE_NAME1, null, contentValues);
return id;
}
public long insertDataCategory(String category) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(DataBaseTransaction.CATEGORY, category);
// id will be negative if something went wrong otherwise it contains row id
long id = db.insert(DataBaseTransaction.TABLE_NAME2, null, contentValues);
return id;
}
public String getAllDataCategory() {
// saving to ArrayList
//ArrayList<String> arraylist = new ArrayList<String>();
// showing data
SQLiteDatabase db = helper.getWritableDatabase();
String [] columns = {DataBaseTransaction.UID, DataBaseTransaction.CATEGORY};
Cursor cursor = db.query(DataBaseTransaction.TABLE_NAME2, columns, null, null, null, null, null);
StringBuffer buffer = new StringBuffer();
while (cursor.moveToNext())
{
// behtare bejaye 0 az int index = cursor.getColumnIndex(DataBaseTransaction.UID)
int cid = cursor.getInt(0);
String category = cursor.getString(cursor.getColumnIndex(DataBaseTransaction.CATEGORY));
//arraylist.add(category);
buffer.append(cid + " " + category + "\n");
}
return buffer.toString();
//return Integer.toString(5);
//return arraylist;
}
static class DataBaseTransaction extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "hulkdb";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME1 = "transaction";
private static final String TABLE_NAME2 = "category";
private static final String UID = "_id";
private static final String TRANSACTION = "usertransaction";
private static final String CATEGORY = "category";
private static final String CREATE_TABLE1 = "CREATE TABLE " + TABLE_NAME1
+ " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + TRANSACTION
+ " INTEGER);";
private static final String CREATE_TABLE2 = "CREATE TABLE "+ TABLE_NAME2 +" ("+ UID +" INTEGER PRIMARY KEY AUTOINCREMENT, "+CATEGORY+" VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS "
+ TABLE_NAME1;
public DataBaseTransaction(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
// CREATE TABLE transaction (_id INTEGER PRIMARY KEY AUTOINCREMENT, transaction INTEGER);
db.execSQL(CREATE_TABLE1);
// CREATE TABLE category (_id INTEGER PRIMARY KEY AUTOINCREMENT, category VARCHAR(255));
db.execSQL(CREATE_TABLE2);
}
catch (SQLException e) {
Log.e("ERROR","SQL on create");
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Log.e("ERROR","SQL on upgrade");
}
}
}
}
公共类数据库TransactionAdapter{
数据库事务助手;
公共数据库TransactionAdapter(上下文){
helper=新数据库事务(上下文);
}
公共长插入数据事务(int事务){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
contentValues.put(DataBaseTransaction.TRANSACTION,TRANSACTION);
//如果出现错误,id将为负值,否则它包含行id
long id=db.insert(DataBaseTransaction.TABLE_NAME1,null,contentValues);
返回id;
}
公共长插入数据类别(字符串类别){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues ContentValues=新ContentValues();
contentValues.put(DataBaseTransaction.CATEGORY,CATEGORY);
//如果出现错误,id将为负值,否则它包含行id
long id=db.insert(DataBaseTransaction.TABLE_NAME2,null,contentValues);
返回id;
}
公共字符串getAllDataCategory(){
//保存到ArrayList
//ArrayList ArrayList=新的ArrayList();
//显示数据
SQLiteDatabase db=helper.getWritableDatabase();
String[]columns={DataBaseTransaction.UID,DataBaseTransaction.CATEGORY};
Cursor Cursor=db.query(DataBaseTransaction.TABLE_NAME2,columns,null,null,null,null);
StringBuffer=新的StringBuffer();
while(cursor.moveToNext())
{
//behtare bejaye 0 az int index=cursor.getColumnIndex(DataBaseTransaction.UID)
int-cid=cursor.getInt(0);
字符串类别=cursor.getString(cursor.getColumnIndex(DataBaseTransaction.category));
//arraylist.add(类别);
buffer.append(cid+“”+category+“\n”);
}
返回buffer.toString();
//返回整数.toString(5);
//返回数组列表;
}
静态类DataBaseTransaction扩展了SQLiteOpenHelper{
私有静态最终字符串数据库_NAME=“hulkdb”;
私有静态最终int数据库_VERSION=1;
私有静态最终字符串表_NAME1=“事务”;
私有静态最终字符串表_NAME2=“category”;
私有静态最终字符串UID=“\u id”;
私有静态最终字符串TRANSACTION=“usertransaction”;
私有静态最终字符串CATEGORY=“CATEGORY”;
私有静态最终字符串CREATE_TABLE1=“CREATE TABLE”+TABLE_NAME1
+(“+UID+”整型主键自动递增,“+TRANSACTION”
+“整数”;“;
私有静态最终字符串CREATE_TABLE2=“CREATE TABLE”+TABLE_NAME2+”(“+UID+”整型主键自动递增“+CATEGORY+”VARCHAR(255));”;
私有静态最终字符串DROP\u TABLE=“DROP TABLE IF EXISTS”
+表1;
公共数据库事务(上下文){
super(上下文、数据库名称、null、数据库版本);
}
@凌驾
public void onCreate(SQLiteDatabase db){
试一试{
//创建表事务(_idinteger主键自动递增,事务整数);
db.execSQL(创建表1);
//创建表类别(_idinteger主键自动递增,类别VARCHAR(255));
db.execSQL(创建表2);
}
捕获(SQLE异常){
Log.e(“错误”,“创建时SQL”);
}
}
@凌驾
public void onUpgrade(SQLiteDatabase db,int-oldVersion,int-newVersion){
试一试{
db.execSQL(DROP_TABLE);
onCreate(db);
}捕获(SQLE异常){
Log.e(“错误”,“升级时的SQL”);
}
}
}
}
LogCat中的错误是:没有这样的表:category
这行代码存在问题:游标游标=db.query(DataBaseTransaction.TABLE_NAME2,columns,null,null,null,null)删除并重新安装应用程序1。onUpgrage只删除表1,但一旦创建,则同时创建表1和表2;2.放下:如果存在放下桌子…谢谢Alexander Zhak它成功了!!