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

古迪

因此,我正在尝试构建一个用户创建事件并将事件存储到数据库的应用程序。但是,每当我单击“保存”按钮时,应用程序就会崩溃。我已经尝试了很多方法来尝试解决该问题,但是我一直在失败。这是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", "[email protected]");
    insertUser(db, "Yesmine", "nadalind", "[email protected]");

    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()));

}}

潜水员

您不能使用封闭的游标:

public int getNotesCount() { 
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from Notes", null);
    int count = cursor.getCount()
    cursor.close();

    return count;
} 

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

当我尝试加载数据库时,应用程序不断崩溃

C#。当我尝试检查用户是否存在于我的 SQL 数据库中时,我的 Windows 窗体应用程序崩溃

当我尝试从 SQLite 数据库中删除一行时,为什么我的 android 应用程序崩溃?科特林

当我在我的应用程序中添加 Firebase 实时数据库时它开始崩溃

当我在数据库查询中放置第4列时,我的应用程序崩溃

尝试将记录插入数据库时 Android Studio 应用程序崩溃,错误消息没有意义

如何获得从SQLite数据库中选择的特定值?当我单击按钮时,应用程序崩溃

当我尝试写入或更新到SQLite数据库时,我的通讯录应用程序停止

在Android的SQLite数据库中插入数据时,应用程序崩溃

REST PHP API尝试从Android应用程序将数据插入数据库

尝试打开sqlite数据库后,我的android应用程序崩溃

尝试保存到 SQLite 数据库时应用程序崩溃

外部数据库访问,我的应用程序崩溃

访问数据库时应用程序崩溃

数据库应用程序崩溃

为什么我的应用程序在打开数据库时崩溃?

每当我在该SetupActivity中单击“保存”按钮时,我的应用就会崩溃。每当我要在Firebase数据库中写入时,我的应用程序崩溃

当我尝试将数据从一个活动传递到另一个活动时,我的应用程序崩溃

尝试将数据从活动传递到片段时,我的应用程序崩溃

从Web API将数据插入SQLite数据库后,为什么Xamarin.Forms应用程序崩溃?

当我尝试打开蓝牙时应用程序崩溃

当我尝试调试时应用程序崩溃

当我尝试打开 Fragment 时应用程序崩溃

当我尝试设置onClickListener时,应用程序崩溃

尝试使用asp.net Web应用程序将数据插入SQL Server数据库,出现错误

当我尝试在 MySQL 中将数据插入数据库时遇到问题

当我更改数据库时,为什么我的 React 应用程序在 useEffect 中运行 firebase 查询?

应用程序由于将数据添加到数据库而崩溃

当我启动 spring boot 应用程序时,我的数据库没有启动