当我尝试将数据插入数据库时​​,应用程序崩溃

时间:2016-07-27 10:52:13

标签: android

所以,我正在尝试构建一个用户创建事件的应用程序,并将事件存储到数据库中。但每次单击“保存”按钮时,应用程序都会崩溃。我尝试了许多尝试来破译问题,但我经常失败。 这是SQLiteOpenHelper的代码

  public class AccessDatabase extends SQLiteOpenHelper {


// Declaring the variables
private static final String DB_NAME = "unilog";
private static final int DB_VERSION = 2;
private static final String TB_NOTES = "Notes";
private static final String NOTE_NAME = "name";
private static final String NOTE_DATE = "date";
private static final String NOTE_LOCATION = "location";
private static final String NOTE_DESCRIPTION = "description";
private static final String NOTE_STATUS = "status";
private static final String NOTE_CREATION = "createdby";


private static final String CREATE_NOTES_TABLE = "CREATE TABLE " + TB_NOTES + " ("
        + " num INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_NAME + " TEXT,"
        + NOTE_DATE + " TEXT," + NOTE_LOCATION + " TEXT," + NOTE_DESCRIPTION + " TEXT," + NOTE_CREATION + " TEXT," + NOTE_STATUS + " INTEGER)";

private static final String CREATE_USER_TABLE = "CREATE TABLE USERS ("
        + "id INTEGER PRIMARY KEY AUTOINCREMENT, "
        + "NAME TEXT NOT NULL, "
        + "PASSWORD TEXT NOT NULL, "
        + "EMAIL TEXT NOT NULL) ";
//Done Declaring the variables

AccessDatabase(Context context) {

    super(context, DB_NAME, null, DB_VERSION);
}

private static void insertUser(SQLiteDatabase db, String name, String password, String email) {
    ContentValues user = new ContentValues();
    user.put("NAME", name);
    user.put("PASSWORD", password);
    user.put("EMAIL", email);
    db.insert("USERS", null, user);


}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_NOTES_TABLE);
    db.execSQL(CREATE_USER_TABLE);

    insertUser(db, "Yosr", "123456789", "yosrgouddi@gmail.com");
    insertUser(db, "Yesmine", "nadalind", "yesminegouddi@gmail.com");

    ContentValues io = new ContentValues();
    io.put(NOTE_NAME, "Yosr");
    io.put(NOTE_DATE, "121212");
    io.put(NOTE_LOCATION, "Unilog");
    io.put(NOTE_DESCRIPTION, "Android");
    io.put(NOTE_CREATION, "By me");
    io.put(NOTE_STATUS, 0);
    db.insert(TB_NOTES, null, io);


}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TB_NOTES);
    db.execSQL("DROP TABLE IF EXISTS USERS");

    onCreate(db);
}


//TOdo add note
public void addNote(Notes note) {
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(NOTE_NAME, note.getName());
    values.put(NOTE_DATE, note.getEventDate());
    values.put(NOTE_LOCATION, note.getEventLocation());
    values.put(NOTE_DESCRIPTION, note.getEventDescription());
    values.put(NOTE_CREATION, note.getCreatedBy());
    values.put(NOTE_STATUS, note.getStatus());
    //todo insert row
    db.insert(TB_NOTES, null, values);
    db.close();

}
//TODO : GETTING A SINGLE NOTE

public Notes getNote(String name, String date) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.query(TB_NOTES, new String[]{NOTE_NAME, NOTE_DATE, NOTE_LOCATION, NOTE_DESCRIPTION, NOTE_CREATION, NOTE_STATUS},
            NOTE_NAME + " =? AND " + NOTE_DATE + " =?", new String[]{name, date}, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
    Notes note = new Notes(cursor.getString(0), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getInt(5));
    cursor.close();
    return note;
}


//TODO : GETTING ALL NOTES

public List<Notes> getallnotes() {
    List<Notes> notesList = new ArrayList<Notes>();
    String SelectQuery = "SELECT * FROM " + TB_NOTES;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(SelectQuery, null);
    if (cursor.moveToFirst()) {
        do {
            Notes note = new Notes();
            note.setName(cursor.getString(1));
            note.setEventDate(cursor.getString(2));
            note.setDescription(cursor.getString(4));
            note.setEventLocation(cursor.getString(3));
            note.setCreatedBy(cursor.getString(5));
            notesList.add(note);
        } while (cursor.moveToNext());
    }
    cursor.close();
    return notesList;
}

//TODO delete note

public void DeleteNote(Notes notes)

{
    SQLiteDatabase db = this.getWritableDatabase();
    db.delete(TB_NOTES, "(" + NOTE_NAME + " = ? AND " + NOTE_DATE + "= ?)",
            new String[]{notes.getName(), notes.getEventDate()});
    db.close();
}

//Get number of notes
public int getNotesCount() {
    String countQuery = "SELECT  * FROM " + TB_NOTES;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from Notes", null);

    cursor.close();

    // return count
    return cursor.getCount();
}

} 这里是note.java的代码

 public class Notes {
 private int status;
 private String Name, EventDate,EventLocation,EventDescription ,    createdBy;
public Notes()
{}

public Notes (String name, int status)
{
    this.Name = name;
    this.status = status;

}
public Notes(String name,String EventDate, String EventLocation, String Description, String createdby, int status) {

    this.createdBy = createdby;
    this.EventDescription= Description;
    this.EventLocation= EventLocation;
    this.EventDate=EventDate;
    this.Name = name;
    this.status = status;
}

// setters

//TODO NAME
public void setName(String name) {
    this.Name = name;
}
//TODO DESCRIPTION
public void setDescription (String description) {
    this.EventDescription = description;
}

//TODO LOCATION
public void setEventLocation (String location)
{
    this.EventLocation = location;
}

//TODO STATUS
public void setStatus(int status) {
    this.status = status;
}
//TODO EVENTDATE
public void setEventDate(String date)
{this.EventDate = date;}

//TODO CREATEDBY
public void setCreatedBy(String createdby){
    this.createdBy = createdby;
}

// getters

//TODO NAME
public String getName() {
    return this.Name;
}
//TODO STATUS
public int getStatus() {
    return this.status;
}
//TODO CREATEDBY

public String getCreatedBy() {
    return createdBy;
}

//TODO EVENTDATE
public java.lang.String getEventDate() {
    return EventDate;
}
//TODO EVENTLOCATION
public java.lang.String getEventLocation() {
    return EventLocation;
}

public String getEventDescription() {
    return EventDescription;
}

}

最后是createnote.java

  public class CreateNote extends Activity {

//TODO Declaring Variables
 String name;

TextView enddate, startdate, datetext;
Button display, SaveEvent, CancelEvent;
ImageButton StartDate, EndDate, datebutton;
EditText eventlocation, eventdescription, newnote;
Calendar mycalendar1 = Calendar.getInstance();
Calendar mycalendar2 = Calendar.getInstance();
Calendar mytime = Calendar.getInstance();
AccessDatabase db = new AccessDatabase(this);
//TODO  Done declaring variables

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



    //TODO:EditText : location & description
    eventlocation = (EditText) findViewById(R.id.location);
    eventdescription = (EditText) findViewById(R.id.description);
    String location = eventlocation.getText().toString();
    String description = eventdescription.getText().toString();
    eventlocation.setText(location, TextView.BufferType.EDITABLE);
    eventdescription.setText(description, TextView.BufferType.EDITABLE);
    newnote  = (EditText)findViewById(R.id.newnote);
    name = newnote.getText().toString();

    //TODO save and cancel buttons

     SaveEvent = (Button)findViewById(R.id.savenote);
     CancelEvent = (Button)findViewById(R.id.cancelnote);
    SaveEvent.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
         //   String msg = db.getNote("Yosr", "121212").getCreatedBy();

                  int non  = db.getNotesCount();
                      StringBuilder stro = new StringBuilder();
                  stro.append(non);
                    String nin = stro.toString();


                Toast.makeText(getApplicationContext(), nin,  Toast.LENGTH_LONG).show();

            }
        });




            //TODO :Buttons : start & end & date


    datebutton = (ImageButton) findViewById(R.id.datebutton);
    StartDate = (ImageButton) findViewById(R.id.dpresultday);
    EndDate = (ImageButton) findViewById(R.id.dpendtime);


    //TODO TextView : startdate and enddate
    datetext = (TextView) findViewById(R.id.date);
    startdate = (TextView) findViewById(R.id.startdate);
    enddate = (TextView) findViewById(R.id.enddate);


    //TODO Calendar and datepicker stuff for start

    final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
            mytime.set(Calendar.YEAR, year);
            mytime.set(Calendar.MONTH, month);
            mytime.set(Calendar.DAY_OF_MONTH, day);
            updatelabel2();
        }
    };

    //TODO Startdate button listener
    StartDate.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View view) {
            int hour = mycalendar1.get(Calendar.HOUR_OF_DAY);
            int minute = mycalendar2.get(Calendar.MINUTE);
            TimePickerDialog mtimepicker;
            mtimepicker = new TimePickerDialog(CreateNote.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedhour, int selectedminute) {

                  startdate.setText(selectedhour + ":" + selectedminute);

                }
            }, hour, minute, true);
            mtimepicker.setTitle("selecttime");
            mtimepicker.show();
        }

    });


  //TODO TimePicker listener for startButton

    datebutton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            new DatePickerDialog(CreateNote.this, date, mycalendar1
                    .get(Calendar.YEAR), mytime.get(Calendar.MONTH),
                    mytime.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

    //TODO Enddate button listener
    EndDate.setOnClickListener(new View.OnClickListener() {
        @Override

        public void onClick(View view) {
            int hour = mycalendar2.get(Calendar.HOUR_OF_DAY);
            int minute = mycalendar2.get(Calendar.MINUTE);
            TimePickerDialog mtimepicker;
            mtimepicker = new TimePickerDialog(CreateNote.this, new TimePickerDialog.OnTimeSetListener() {
                @Override
                public void onTimeSet(TimePicker timePicker, int selectedhour, int selectedminute) {
                    enddate.setText(selectedhour + ":" + selectedminute);
                }
            }, hour, minute, true);
            mtimepicker.setTitle("selecttime");
            mtimepicker.show();

        }
    });




}




private void updatelabel2(){

    String myFormat = "MM/dd/yy"; //In which you need put here
    SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);

    datetext.setText(sdf.format(mytime.getTime()));

} }

1 个答案:

答案 0 :(得分:0)

您无法使用关闭的光标:

Saved app_id, writing to ionic.io.bundle.min.js...
An error occurred uploading the build: Error: socket hang up

Error: socket hang up
    at createHangUpError (_http_client.js:202:15)
    at TLSSocket.socketOnEnd (_http_client.js:287:23)
    at emitNone (events.js:72:20)
    at TLSSocket.emit (events.js:166:7)
    at endReadableNT (_stream_readable.js:905:12)
    at nextTickCallbackWith2Args (node.js:441:9)
    at process._tickCallback (node.js:355:17)

socket hang up (CLI v1.7.16)