我正在制作应用程序以列表形式插入数据和检索数据..但我没有得到任何列表并得到空白页。 logcat没有给出任何错误。
代码:Components.java
public class Components extends Activity implements OnClickListener {
Button b1,b2,b3,b4;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_components);
Intent i = getIntent();
b1= (Button) findViewById(R.id.mobile);
b2= (Button) findViewById(R.id.camera);
b3= (Button) findViewById(R.id.computer);
b4= (Button) findViewById(R.id.bike);
b1.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.mobile) {
Sqlite sqll = new Sqlite(this);
sqll.insertMobile();
Intent h = new Intent(Components.this, Mobile.class);
startActivity(h);
}
}
}
代码:Sqlite.java
public class Sqlite extends SQLiteOpenHelper {
public static final String DB_NAME = "Get_Price";
public static final int DB_VERSION = 1;
public static final String MTABLE_NAME = "mobile";
public static final String COLUMN_MNAME= "mname";
public static final String COLUMN_MMODEL= "model";
public static final String COLUMN_MPRICE= "mprice";
public static final String COLUMN_MCAMERA= "mcamera";
public static final String CTABLE_NAME = "camera";
public static final String COLUMN_CNAME= "cname";
public static final String COLUMN_CMODEL= "cmodel";
public static final String COLUMN_CPRICE= "cprice";
public static final String COLUMN_CCAMERA= "ccamera";
public static final String BTABLE_NAME = "bike";
public static final String COLUMN_BNAME= "bname";
public static final String COLUMN_BMODEL= "bmodel";
public static final String COLUMN_BPRICE= "bprice";
public static final String LTABLE_NAME = "laptop";
public static final String COLUMN_LNAME= "lname";
public static final String COLUMN_LMODEL= "lmodel";
public static final String COLUMN_LPRICE= "lprice";
public Sqlite(Context context) {
super(context, DB_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
String CreateTableMoblie = "CREATE TABLE " + MTABLE_NAME + " ( " + COLUMN_MNAME + " TEXT, " + COLUMN_MMODEL + " TEXT "
+ COLUMN_MPRICE + " TEXT, " + COLUMN_MCAMERA + " TEXT " + " ) ";
db.execSQL(CreateTableMoblie);
String CreateTableCamera = "CREATE TABLE " + CTABLE_NAME + " ( " + COLUMN_CNAME + " TEXT, " + COLUMN_CMODEL + " TEXT "
+ COLUMN_CPRICE + " TEXT, " + COLUMN_CCAMERA + " TEXT " + " ) ";
db.execSQL(CreateTableCamera);
String CreateTableBike = "CREATE TABLE " + BTABLE_NAME + " ( " + COLUMN_BNAME + " TEXT, " + COLUMN_BMODEL + " TEXT "
+ COLUMN_BPRICE + " TEXT " + " ) ";
db.execSQL(CreateTableBike);
String CreateTableLaptop = "CREATE TABLE " + LTABLE_NAME + " ( " + COLUMN_LNAME + " TEXT, " + COLUMN_LMODEL + " TEXT "
+ COLUMN_LPRICE + " TEXT " + " ) ";
db.execSQL(CreateTableLaptop);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " +MTABLE_NAME );
db.execSQL("DROP TABLE IF EXISTS " +CTABLE_NAME );
db.execSQL("DROP TABLE IF EXISTS " +BTABLE_NAME );
db.execSQL("DROP TABLE IF EXISTS " +LTABLE_NAME );
onCreate(db);
}
public long insertMobile() {
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("mname","samsung" );
values.put("mmodel","samsung2s" );
values.put("mprice",5000 );
values.put("mcamera","yes" );
database.insert("mobile", null, values);
values.put("mname","nokia" );
values.put("mmodel","nokia1100" );
values.put("mprice",2000 );
values.put("mcamera","no" );
database.insert("mobile", null, values);
values.put("mname","sony xperia" );
values.put("mmodel","xperia100" );
values.put("mprice","30000" );
values.put("mcamera","yes" );
return database.insert("mobile", null, values);
}
}
代码:Mobile.java
public class Mobile extends ListActivity {
Sqlite sql = new Sqlite(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mobile);
Intent h= getIntent();
ArrayList<HashMap<String, String>> MobileList = getAllMobiles();
ListAdapter adapter = new SimpleAdapter(Mobile.this, MobileList, R.layout.mobile_names,
new String[] {Sqlite.COLUMN_MNAME}, new int[] {R.id.nameofmobile});
setListAdapter(adapter);
}
public ArrayList<HashMap<String,String>> getAllMobiles() {
ArrayList <HashMap<String,String>> wordlist;
wordlist= new ArrayList <HashMap<String, String>>();
String Query = "SELECT * FROM mobile";
SQLiteDatabase database = sql.getWritableDatabase();
Cursor cursor = database.rawQuery(Query, null);
if (cursor.moveToFirst()) {
do {
HashMap<String,String> map = new HashMap<String, String>();
map.put(Sqlite.COLUMN_MNAME, cursor.getString(0));
map.put(Sqlite.COLUMN_MMODEL, cursor.getString(1));
map.put(Sqlite.COLUMN_MPRICE, cursor.getString(2));
map.put(Sqlite.COLUMN_MCAMERA, cursor.getString(3));
wordlist.add(map);
} while (cursor.moveToNext());
}
return wordlist;
}
}
logcat的:
04-03 12:11:32.774: E/SQLiteLog(609): (1) table mobile has no column named mprice
04-03 12:11:32.804: E/SQLiteDatabase(609): Error inserting mname=samsung mcamera=yes mprice=5000 mmodel=samsung2s
04-03 12:11:32.804: E/SQLiteDatabase(609): android.database.sqlite.SQLiteException: table mobile has no column named mprice (code 1): , while compiling: INSERT INTO mobile(mname,mcamera,mprice,mmodel) VALUES (?,?,?,?)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
04-03 12:11:32.804: E/SQLiteDatabase(609): at com.example.get_price.Sqlite.insertMobile(Sqlite.java:85)
04-03 12:11:32.804: E/SQLiteDatabase(609): at com.example.get_price.Components.onClick(Components.java:32)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.view.View.performClick(View.java:4084)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.view.View$PerformClick.run(View.java:16966)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.os.Handler.handleCallback(Handler.java:615)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.os.Handler.dispatchMessage(Handler.java:92)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.os.Looper.loop(Looper.java:137)
04-03 12:11:32.804: E/SQLiteDatabase(609): at android.app.ActivityThread.main(ActivityThread.java:4745)
04-03 12:11:32.804: E/SQLiteDatabase(609): at java.lang.reflect.Method.invokeNative(Native Method)
04-03 12:11:32.804: E/SQLiteDatabase(609): at java.lang.reflect.Method.invoke(Method.java:511)
04-03 12:11:32.804: E/SQLiteDatabase(609): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-03 12:11:32.804: E/SQLiteDatabase(609): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-03 12:11:32.804: E/SQLiteDatabase(609): at dalvik.system.NativeStart.main(Native Method)
我收到此错误..
答案 0 :(得分:0)
table mobile没有名为mprice的列
您在mmodel和mprice之间错过了,
逗号:
String CreateTableMoblie = "CREATE TABLE " + MTABLE_NAME + " ( " + COLUMN_MNAME + " TEXT, " + COLUMN_MMODEL + " TEXT "
+ COLUMN_MPRICE + " TEXT, " + COLUMN_MCAMERA + " TEXT " + " ) ";
应该是
String CreateTableMoblie = "CREATE TABLE " + MTABLE_NAME + " ( " + COLUMN_MNAME + " TEXT, " + COLUMN_MMODEL + " TEXT, "
+ COLUMN_MPRICE + " TEXT, " + COLUMN_MCAMERA + " TEXT " + " ) ";
在此处更改架构后,例如卸载您的应用,以便删除旧的数据库文件。