我是Android新手,并且正在尝试使用SQLite,但我现在卡住了。每次我启动我的应用程序时,应用程序崩溃。我只想在点击按钮时向数据库添加一个值,但它不起作用。
你能帮帮我吗?
这是我的MainActivity 进口[..]
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnStarzeit).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startSpeichern(1);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void startSpeichern(int value){
DatabaseHandler dbHandler = new DatabaseHandler(this,null,null,1);
dbHandler.neueStartzeit(value);
}
}
这是我的DatabaseHandler
imports[..]
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 9;
private static final String DATABASE_NAME = "Name der Datenbank";
public static final String TABLE_PRODUCTS = "produkte";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_PRODUCTNAME = "productname";
public static final String COLUMN_QUANTITY="menge";
public DatabaseHandler (Context context, String name, SQLiteDatabase.CursorFactory factory,int version){
super(context,DATABASE_NAME,factory,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db){
String CREATE_PRODUCTS_TABLE = "CREATE TABLE " +
TABLE_PRODUCTS + "("
+ COLUMN_ID + " INTEGER PRIMARY KEY," + COLUMN_PRODUCTNAME
+ " INTEGER," + COLUMN_QUANTITY + " INTEGER" + ")";
db.execSQL(CREATE_PRODUCTS_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_PRODUCTS);
onCreate(db);
}
public void neueStartzeit(){
ContentValues values = new ContentValues();
values.put(COLUMN_PRODUCTNAME,1);
values.put(COLUMN_QUANTITY,1);
SQLiteDatabase db = this.getWritableDatabase();
db.insert(TABLE_PRODUCTS,null, values);
db.close();
}
}
日志
ddmlib: Broken pipe
java.io.IOException: Broken pipe
at sun.nio.ch.FileDispatcherImpl.write0(Native Method)
at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:47)
at sun.nio.ch.IOUtil.writeFromNativeBuffer(IOUtil.java:93)
at sun.nio.ch.IOUtil.write(IOUtil.java:65)
at sun.nio.ch.SocketChannelImpl.write(SocketChannelImpl.java:487)
at com.android.ddmlib.JdwpPacket.writeAndConsume(JdwpPacket.java:213)
at com.android.ddmlib.Client.sendAndConsume(Client.java:675)
at com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:342)
at com.android.ddmlib.Client.requestAllocationStatus(Client.java:521)
at com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:847)
at com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:815)
at com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMonitor.java:775)
at com.android.ddmlib.DeviceMonitor.deviceClientMonitorLoop(DeviceMonitor.java:664)
at com.android.ddmlib.DeviceMonitor.access$100(DeviceMonitor.java:46)
at com.android.ddmlib.DeviceMonitor$3.run(DeviceMonitor.java:592)