创建数据库不适用于Android / Android Studio

时间:2016-07-30 10:55:20

标签: java android sqlite android-studio

刚开始使用android并尝试创建简单的数据库。

这是我的代码:

HealthyDbHanbler.java

public class HealthyDbHandler extends SQLiteOpenHelper {

public HealthyDbHandler(Context context){
    super(context,FoodMacrosTable.DB_NAME,null,FoodMacrosTable.DB_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String createTable = "CREATE TABLE " + FoodMacrosTable.FoodMacros.TABLE_NAME + " ( " +
            FoodMacrosTable.FoodMacros._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
            FoodMacrosTable.FoodMacros.FOOD_NAME + " TEXT NOT NULL, " +
            FoodMacrosTable.FoodMacros.FOOD_PROTEIN + " TEXT NOT NULL, " +
            FoodMacrosTable.FoodMacros.FOOD_CARBS + " TEXT NOT NULL, " +
            FoodMacrosTable.FoodMacros.FOOD_FATS + " TEXT NOT NULL, " +
            FoodMacrosTable.FoodMacros.FOOD_CALORIES + " TEXT NOT NULL, " +
            FoodMacrosTable.FoodMacros.FOOD_VIT_A + " TEXT NOT NULL );";

    sqLiteDatabase.execSQL(createTable);


}

@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + FoodMacrosTable.FoodMacros.TABLE_NAME);
    onCreate(sqLiteDatabase);
}

public void addFood(Food food){
    SQLiteDatabase db = this.getWritableDatabase();

    ContentValues values =  new ContentValues();
    values.put(FoodMacrosTable.FoodMacros.FOOD_NAME,food.getFOOD_NAME());
    values.put(FoodMacrosTable.FoodMacros.FOOD_PROTEIN,food.getFOOD_PROTEIN());
    values.put(FoodMacrosTable.FoodMacros.FOOD_CARBS,food.getFOOD_CARBS());
    values.put(FoodMacrosTable.FoodMacros.FOOD_FATS,food.getFOOD_FATS());
    values.put(FoodMacrosTable.FoodMacros.FOOD_CALORIES,food.getFOOD_CALORIES());
    values.put(FoodMacrosTable.FoodMacros.FOOD_VIT_A,food.getFOOD_VIT_A());

    db.insert(FoodMacrosTable.FoodMacros.TABLE_NAME,null,values);

    db.close();
}

}

MainActivity.java
public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    HealthyDbHandler db = new HealthyDbHandler(this) ;
    Log.v("DBINFO","Database created");

}

}

{{1}}

}

这是我得到的错误:     1:42:56 PM无法绑定到本地8600调试器     1:42:56 PM已建立的连接被主机中的软件中止     java.io.IOException:已建立的连接已被主机中的软件中止            at sun.nio.ch.SocketDispatcher.write0(Native Method)            at sun.nio.ch.SocketDispatcher.write(SocketDispatcher.java:51)            在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:471)            在com.android.ddmlib.JdwpPacket.write(JdwpPacket.java:179)            在com.android.ddmlib.Client.send(Client.java:654)            在com.android.ddmlib.jdwp.JdwpAgent.send(JdwpAgent.java:92)            在com.android.ddmlib.HandleHeap.sendREAQ(HandleHeap.java:349)            在com.android.ddmlib.Client.requestAllocationStatus(Client.java:517)            在com.android.ddmlib.DeviceMonitor.createClient(DeviceMonitor.java:573)            在com.android.ddmlib.DeviceMonitor.openClient(DeviceMonitor.java:545)            在com.android.ddmlib.DeviceMonitor.processIncomingJdwpData(DeviceMoni ...(显示气球)

1 个答案:

答案 0 :(得分:0)

此错误与sqlite甚至您的应用程序无关。

尝试重新启动Android Studio,adb服务器, 在您的设备中重新启用USB调试,或从相关主题中重新启动(如果没有任何帮助) Eclipse DDMS error "Can't bind to local 8600 for debugger"