我有Sqlite数据库,其中包含4个表,其中两个包含我已插入的静态数据,但是当我运行应用程序时,它显示该表但为空...
这是我的Sqlite文件...
{
// Database Version
private static final int DATABASE_VERSION = 2;
Context context;
// Database Name
private static final String DATABASE_NAME = "DynamicERP.db";
public static final String table_imei = "IMEITABLE";
public static final String table_login= "USERLOGIN";
public static final String table_orders = "TABLEORDERS";
public static final String table_poducts_records = "PRODUCTSRECORDS";
// User Table Columns names
public static final String imeiid = "IMEIID";
public static final String imei = "IMEI";
public static final String userid = "USERID";
public static final String username = "USERNAME";
public static final String password = "PASSWORD";
public static final String userrole = "USERROLE";
public static final String code = "ORDERCODE";
public static final String barcode = "BARCODE";
public static final String nametblp = "NAME";
public static final String size = "SIZE";
public static final String uname = "UNAME";
public static final String retail = "RETAIL";
public static final String wholesale = "WHOLESALE";
public static final String trade = "TRADE";
public static final String percentage = "PERCENTAGE";
public static final String tax = "TAX";
public static final String subcatagary = "SUBCATAGARY";
public static final String categary = "CATEGARY";
public static final String company = "COMPANY";
public static final String type = "TYPE";
public static final String packsize = "PACKSIZE";
public static final String weight = "WEIGHT";
public static final String weightunit = "WEIGHTUNIT";
public static final String currentstock = "CURRENTSTOCK";
public static final String salecode = "SALECODE";
public static final String mcode = "MCODE";
public static final String fname = "FIRSTNAME";
public static final String lname = "LASTNAME";
public static final String urduname = "URDUNAME";
public static final String address = "ADDRESS";
public static final String contact = "CONTACT";
public static final String cnic = "CNIC";
public static final String saletype = "TYPE";
public static final String route = "ROUTE";
public static final String area = "AREA";
public static final String zone = "ZONE";
public static final String balance = "BALANCE";
public static final String creditlimit = "CREDITLIMIT";
public static final String spl = "SPL";
public static final String saletax = "SALETAX";
// create table sql query
private static final String DATABASE_CIMEI = "CREATE TABLE " + table_imei + "("
+ imeiid + " INTEGER, " + imei + " VARCHAR );" ;
private static final String DATABASE_CUSER = "CREATE TABLE " + table_login + "("
+ userid + " INTEGER, " + username + " VARCHAR, " + password + " INTEGER, " + userrole + " VARCHAR );" ;
private static final String DATABASE_ORDERS = "CREATE TABLE " + table_orders + "("
+ code + " INTEGER, " + barcode + " VARCHAR," + nametblp + " VARCHAR," + size + " VARCHAR," + uname +
" VARCHAR," + retail + " VARCHAR," + wholesale + " VARCHAR," + trade + " VARCHAR,"
+ percentage + " VARCHAR," + tax + " VARCHAR," + subcatagary + " VARCHAR," + categary + " VARCHAR," +
company + " VARCHAR," + type + " VARCHAR," + packsize + " VARCHAR," +
weight + " VARCHAR," + weightunit + " VARCHAR, " + currentstock + " VARCHAR );";
private static final String DATABASE_PRODUCTS = "CREATE TABLE " + table_poducts_records + "("
+ salecode + " INTEGER, " + mcode + " VARCHAR, " + fname +" VARCHAR, " + lname +" VARCHAR, " + urduname +
" VARCHAR, " + address +" VARCHAR, " + contact +" VARCHAR," + cnic +" VARCHAR, " + saletype + " VARCHAR,"
+ route + " VARCHAR, " + area + " VARCHAR," + zone + " VARCHAR," +
balance +" VARCHAR, " + creditlimit +" VARCHAR," + spl +" VARCHAR, " + saletax + " VARCHAR);" ;
// drop table sql query
String DROP_IMEI_TABLE = "DROP TABLE IF EXISTS " + table_imei;
private String DROP_USER_TABLE = "DROP TABLE IF EXISTS " + table_login;
private String DROP_ORDER_REC = "DROP TABLE IF EXISTS " + table_orders;
private String DROP_PRODUCTS_REC = "DROP TABLE IF EXISTS " + table_poducts_records;
/**
* Constructor
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CIMEI);
db.execSQL(DATABASE_CUSER);
db.execSQL(DATABASE_ORDERS);
db.execSQL(DATABASE_PRODUCTS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//Drop User Table if exist
db.execSQL(DROP_USER_TABLE);
db.execSQL(DROP_IMEI_TABLE);
db.execSQL(DROP_PRODUCTS_REC);
db.execSQL(DROP_ORDER_REC);
// Create tables aga
onCreate(db);
}
public void addProductRec() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(code, "098766");
values.put(barcode, "5544444445");
values.put(nametblp, "qwuwseman");
values.put(size, "12");
values.put(uname, "uswm");
values.put(retail, "500");
values.put(wholesale, "whole");
values.put(trade, "credit");
values.put(percentage, "7");
values.put(tax, "1000");
values.put(subcatagary, "laptop");
values.put(categary, "computers");
values.put(company, "hp");
values.put(type, "laptop");
values.put(packsize, "12");
values.put(weight, "5");
values.put(weightunit, "kg");
values.put(currentstock, "57");
// Inserting Row
db.insert(table_poducts_records, null, values);
db.close();
}
public void addOrders() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(salecode, "123456");
values.put(mcode, "123654698");
values.put(fname, "Alx");
values.put(lname, "Alx");
values.put(urduname, "Usman");
values.put(address, "waho");
values.put(contact, "021354545");
values.put(cnic, "54548787515");
values.put(saletype, "credit");
values.put(route, "peshqweawerroad");
values.put(area, "waho");
values.put(zone, "rwpq");
values.put(balance, "1000");
values.put(creditlimit, "10000");
values.put(spl, "212");
values.put(saletax, "10");
// Inserting Row
db.insert(table_orders, null, values);
db.close();
}
在线数据库的屏幕截图
在在线数据库中在线检查我的数据之后,数据为空...我很困惑为什么会发生这种情况
这些方法将从这里调用...
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer_card);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
Card1=findViewById(R.id.bankcardId);
Card2=findViewById(R.id.card2);
Card3= findViewById(R.id.card3);
databaseHelper=new DatabaseHelper(this);
Card1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper.addProductRec();
Toast.makeText(NavigationDrawerCardActivity.this, "CARD1", Toast.LENGTH_SHORT).show();
}
});
Card2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
databaseHelper.addOrders();
Toast.makeText(NavigationDrawerCardActivity.this, "Card2", Toast.LENGTH_SHORT).show();
}
});
Card3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(NavigationDrawerCardActivity.this, "Card3", Toast.LENGTH_SHORT).show();
//startActivity(new Intent(NavigationDrawerCardActivity.this,FamilyTable.class));
}
});
这是Logcat
android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
at com.dynamicsolution.pakistan.sql.DatabaseHelper.addOrders(DatabaseHelper.java:199)
at com.dynamicsolution.pakistan.NavigationDrawerCardActivity$2.onClick(NavigationDrawerCardActivity.java:52)
at android.view.View.performClick(View.java:4084)
at android.view.View$PerformClick.run(View.java:16966)
at android.os.Handler.handleCallback(Handler.java:615)
答案 0 :(得分:1)
如果您查看日志,则会发现以下堆栈跟踪,表明 由于列不存在, addOrders 和 addProductRec 方法失败。
您会留言
E/SQLiteLog: (1) table TABLEORDERS has no column named ADDRESS
然后是消息
E/SQLiteLog: (1) table PRODUCTSRECORDS has no column named PERCENTAGE
修复这两个问题(交换它们)会导致更多问题。似乎您必须虚拟交换(如果不是所有列)。
正在更改使用方式:-
public void addProductRec() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(salecode, "123456");
values.put(mcode, "123654698");
values.put(fname, "Alx");
values.put(lname, "Alx");
values.put(urduname, "Usman");
values.put(contact, "021354545");
values.put(cnic, "54548787515");
values.put(saletype, "credit");
values.put(route, "peshqweawerroad");
values.put(area, "waho");
values.put(zone, "rwpq");
values.put(balance, "1000");
values.put(creditlimit, "10000");
values.put(spl, "212");
values.put(saletax, "10");
values.put(address, "waho");
// Inserting Row
db.insert(table_poducts_records, null, values);
db.close();
}
public void addOrders() {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(code, "098766");
values.put(barcode, "5544444445");
values.put(nametblp, "qwuwseman");
values.put(size, "12");
values.put(uname, "uswm");
values.put(retail, "500");
values.put(wholesale, "whole");
values.put(trade, "credit");
values.put(tax, "1000");
values.put(subcatagary, "laptop");
values.put(categary, "computers");
values.put(company, "hp");
values.put(type, "laptop");
values.put(packsize, "12");
values.put(weight, "5");
values.put(currentstock, "57");
values.put(percentage, "7");
values.put(weightunit, "kg");
// Inserting Row
db.insert(table_orders, null, values);
db.close();
}
解决了但问题,即列是否符合预期是另一回事(可能只是您混淆了表)。
运行修改后的代码,并在调用上述方法后运行sql
SELECT sql FROM sqlite_master WHERE type = 'table'
//转储用于创建表的SQL(因此确认表已按预期存在)SELECT * FROM PRODUCTSRECORDS
SELECT * FROM TABLEORDERS
并转储结果Cursor显示:-
05-04 18:37:05.913 5419-5419/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@328ae8f
05-04 18:37:05.913 5419-5419/? I/System.out: 0 {
05-04 18:37:05.913 5419-5419/? I/System.out: sql=CREATE TABLE android_metadata (locale TEXT)
05-04 18:37:05.913 5419-5419/? I/System.out: }
05-04 18:37:05.913 5419-5419/? I/System.out: 1 {
05-04 18:37:05.913 5419-5419/? I/System.out: sql=CREATE TABLE IMEITABLE(IMEIID INTEGER, IMEI VARCHAR )
05-04 18:37:05.913 5419-5419/? I/System.out: }
05-04 18:37:05.914 5419-5419/? I/System.out: 2 {
05-04 18:37:05.914 5419-5419/? I/System.out: sql=CREATE TABLE USERLOGIN(USERID INTEGER, USERNAME VARCHAR, PASSWORD INTEGER, USERROLE VARCHAR )
05-04 18:37:05.914 5419-5419/? I/System.out: }
05-04 18:37:05.914 5419-5419/? I/System.out: 3 {
05-04 18:37:05.914 5419-5419/? I/System.out: sql=CREATE TABLE TABLEORDERS(ORDERCODE INTEGER, BARCODE VARCHAR,NAME VARCHAR,SIZE VARCHAR,UNAME VARCHAR,RETAIL VARCHAR,WHOLESALE VARCHAR,TRADE VARCHAR,PERCENTAGE VARCHAR,TAX VARCHAR,SUBCATAGARY VARCHAR,CATEGARY VARCHAR,COMPANY VARCHAR,TYPE VARCHAR,PACKSIZE VARCHAR,WEIGHT VARCHAR,WEIGHTUNIT VARCHAR, CURRENTSTOCK VARCHAR )
05-04 18:37:05.914 5419-5419/? I/System.out: }
05-04 18:37:05.914 5419-5419/? I/System.out: 4 {
05-04 18:37:05.914 5419-5419/? I/System.out: sql=CREATE TABLE PRODUCTSRECORDS(SALECODE INTEGER, MCODE VARCHAR, FIRSTNAME VARCHAR, LASTNAME VARCHAR, URDUNAME VARCHAR, ADDRESS VARCHAR, CONTACT VARCHAR,CNIC VARCHAR, TYPE VARCHAR,ROUTE VARCHAR, AREA VARCHAR,ZONE VARCHAR,BALANCE VARCHAR, CREDITLIMIT VARCHAR,SPL VARCHAR, SALETAX VARCHAR)
05-04 18:37:05.914 5419-5419/? I/System.out: }
05-04 18:37:05.914 5419-5419/? I/System.out: <<<<<
05-04 18:37:05.914 5419-5419/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@f1c151c
05-04 18:37:05.914 5419-5419/? I/System.out: 0 {
05-04 18:37:05.914 5419-5419/? I/System.out: SALECODE=123456
05-04 18:37:05.914 5419-5419/? I/System.out: MCODE=123654698
05-04 18:37:05.914 5419-5419/? I/System.out: FIRSTNAME=Alx
05-04 18:37:05.914 5419-5419/? I/System.out: LASTNAME=Alx
05-04 18:37:05.914 5419-5419/? I/System.out: URDUNAME=Usman
05-04 18:37:05.914 5419-5419/? I/System.out: ADDRESS=waho
05-04 18:37:05.914 5419-5419/? I/System.out: CONTACT=021354545
05-04 18:37:05.914 5419-5419/? I/System.out: CNIC=54548787515
05-04 18:37:05.914 5419-5419/? I/System.out: TYPE=credit
05-04 18:37:05.914 5419-5419/? I/System.out: ROUTE=peshqweawerroad
05-04 18:37:05.914 5419-5419/? I/System.out: AREA=waho
05-04 18:37:05.914 5419-5419/? I/System.out: ZONE=rwpq
05-04 18:37:05.914 5419-5419/? I/System.out: BALANCE=1000
05-04 18:37:05.914 5419-5419/? I/System.out: CREDITLIMIT=10000
05-04 18:37:05.914 5419-5419/? I/System.out: SPL=212
05-04 18:37:05.914 5419-5419/? I/System.out: SALETAX=10
05-04 18:37:05.914 5419-5419/? I/System.out: }
05-04 18:37:05.914 5419-5419/? I/System.out: <<<<<
05-04 18:37:05.914 5419-5419/? I/System.out: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@d0bb525
05-04 18:37:05.915 5419-5419/? I/System.out: 0 {
05-04 18:37:05.915 5419-5419/? I/System.out: ORDERCODE=98766
05-04 18:37:05.915 5419-5419/? I/System.out: BARCODE=5544444445
05-04 18:37:05.915 5419-5419/? I/System.out: NAME=qwuwseman
05-04 18:37:05.915 5419-5419/? I/System.out: SIZE=12
05-04 18:37:05.915 5419-5419/? I/System.out: UNAME=uswm
05-04 18:37:05.915 5419-5419/? I/System.out: RETAIL=500
05-04 18:37:05.915 5419-5419/? I/System.out: WHOLESALE=whole
05-04 18:37:05.915 5419-5419/? I/System.out: TRADE=credit
05-04 18:37:05.915 5419-5419/? I/System.out: PERCENTAGE=7
05-04 18:37:05.915 5419-5419/? I/System.out: TAX=1000
05-04 18:37:05.915 5419-5419/? I/System.out: SUBCATAGARY=laptop
05-04 18:37:05.915 5419-5419/? I/System.out: CATEGARY=computers
05-04 18:37:05.915 5419-5419/? I/System.out: COMPANY=hp
05-04 18:37:05.915 5419-5419/? I/System.out: TYPE=laptop
05-04 18:37:05.915 5419-5419/? I/System.out: PACKSIZE=12
05-04 18:37:05.915 5419-5419/? I/System.out: WEIGHT=5
05-04 18:37:05.915 5419-5419/? I/System.out: WEIGHTUNIT=kg
05-04 18:37:05.915 5419-5419/? I/System.out: CURRENTSTOCK=57
05-04 18:37:05.915 5419-5419/? I/System.out: }
05-04 18:37:05.915 5419-5419/? I/System.out: <<<<<
即
用于测试和产生以上结果的代码:-
public class MainActivity extends AppCompatActivity {
DatabaseHelper mMyDBHlpr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMyDBHlpr = new DatabaseHelper(this);
mMyDBHlpr.addOrders();
mMyDBHlpr.addProductRec();
DatabaseUtils.dumpCursor(
mMyDBHlpr.getWritableDatabase().rawQuery("SELECT sql FROM sqlite_master WHERE type = 'table'",null)
);
DatabaseUtils.dumpCursor(
mMyDBHlpr.getWritableDatabase().rawQuery("SELECT * FROM PRODUCTSRECORDS",null)
);
DatabaseUtils.dumpCursor(
mMyDBHlpr.getWritableDatabase().rawQuery("SELECT * FROM TABLEORDERS",null)
);
}