当我尝试使用sqlite将数据插入表中时,Android应用程序关闭

时间:2017-03-05 09:32:49

标签: android sqlite

我正在开发一个应用程序,我可以将数据插入数据库。当我按下添加按钮时,数据被插入到数据库中。但是当我按下添加按钮时,应用程序停止工作并返回主页。我附上了UI屏幕截图和代码。

这是数据库架构代码:

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.provider.BaseColumns;
 import android.support.v7.app.AppCompatActivity;
 import android.os.Bundle;
 import android.database.sqlite.SQLiteOpenHelper;

public class DB_EVENTS extends SQLiteOpenHelper {
public static final String db_name="Events.db";
public static final String table_name="eventstable";
public static final String col="ID";
public static final String col1="Event name";
public static final String col2="Price ";
public static final String col3="Date ";
public static final String col4="Capacity";
public static final String col5="Promocode";

//String test1=Createevent.msg;

public DB_EVENTS(Context context) {
    super(context,db_name, null,1);
  //  SQLiteDatabase db=this.getWritableDatabase();
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table"+table_name+"(ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRICE TEXT,DATE TEXT,CAPACITY TEXT,PROMOCODE TEXT)");
}

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

}
 public boolean insert_data(String name,String price ,String date,String     capacity,String promocode){
 SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(col1,name);
contentValues.put(col2,price);
contentValues.put(col3,promocode);
contentValues.put(col4,date);
contentValues.put(col5,capacity);

long result=db.insert(table_name,null,contentValues);
if(result==-1)
{
    return false;
}
else{
    return true;
}
}

 }

这是主要的活动代码:

import android.content.Intent;
import android.provider.BaseColumns;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
 import android.widget.EditText;
 import android.widget.Toast;


 public class Createevent extends AppCompatActivity {
 DB_EVENTS db;


public static final String msg1 = " ";
public static final String msg2 = " ";
public static final String msg3 = " ";
public static final String msg4 = " ";
public static final String msg5 = " ";
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_createevent);
    db=new DB_EVENTS(this);

}
public void add(View view)
{


    EditText editText1 = (EditText) findViewById(R.id.editText);
    String message1 = editText1.getText().toString();
    EditText editText2 = (EditText) findViewById(R.id.editText3);
    String message2 = editText2.getText().toString();
    EditText editText3 = (EditText) findViewById(R.id.editText4);
    String message3 = editText3.getText().toString();
    EditText editText4 = (EditText) findViewById(R.id.editText5);
    String message4 = editText4.getText().toString();
    EditText editText5 = (EditText) findViewById(R.id.editText6);
    String message5 = editText5.getText().toString();
     boolean   isinserted=db.insert_data(message1,message2,message3,
      message4,message5);
    if(isinserted==true){

     Toast.makeText(Createevent.this,"Data  inserted"
        ,Toast.LENGTH_LONG).show();
    }
    else{Toast.makeText(Createevent.this,
    "Data not  inserted",Toast.LENGTH_LONG).show();
    }



}

}


  This is the UI code:
    [![This is the screenshot of the UI of my app][1]][1]
     <?xml version="1.0" encoding="utf-8"?>
     <RelativeLayout  xmlns:android=
    "http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
      android:id="@+id/activity_createevent"
      android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:paddingBottom="@dimen/activity_vertical_margin"
     android:paddingLeft="@dimen/activity_horizontal_margin"
     android:paddingRight="@dimen/activity_horizontal_margin"
     android:paddingTop="@dimen/activity_vertical_margin"
     tools:context="com.example.parth.eventmgmtsystem.Createevent">

    <TextView
    android:text="Fill in the details to create an event"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:id="@+id/textView"
    android:layout_alignParentStart="true"
    android:layout_marginStart="24dp"
    android:layout_alignParentTop="true" />

    <TextView
    android:text="Price"
    android:layout_width="100dp"
    android:layout_height="wrap_content"
    android:layout_below="@+id/editText"
    android:layout_alignStart="@+id/textView4"
    android:layout_marginTop="31dp"
    android:id="@+id/textView5" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="20dp"
    android:inputType="textPersonName"
    android:text=" "
    android:ems="10"
    android:layout_alignBottom="@+id/textView5"
    android:layout_toEndOf="@+id/textView5"
    android:layout_marginStart="36dp"
    android:id="@+id/editText3" />

    <TextView
    android:text="Date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="39dp"
    android:id="@+id/textView6"
    android:layout_below="@+id/textView5"
    android:layout_alignStart="@+id/textView5" />

   <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/editText4"
    android:layout_alignBaseline="@+id/textView6"
    android:layout_alignBottom="@+id/textView6"
    android:layout_toEndOf="@+id/textView" />

   <Button
    android:text="ADD"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_toEndOf="@+id/textView5"
    android:layout_marginBottom="13dp"
    android:id="@+id/button3"
    android:onClick="add"/>

    <TextView
    android:text="Capacity"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView7"
    android:layout_centerVertical="true" />

    <TextView
    android:text="Promocode"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="64dp"
    android:id="@+id/textView8"
    android:layout_below="@+id/textView7"
    android:layout_alignParentStart="true" />

    <TextView
    android:text="Enter the name of the event"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView3"
    android:layout_marginTop="15dp"
    android:layout_below="@+id/textView"
    android:layout_alignEnd="@+id/button3"
    android:layout_marginEnd="29dp" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/editText"
    android:layout_alignTop="@+id/textView3"
    android:layout_alignParentEnd="true"
    android:layout_toEndOf="@+id/button3" />

   <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text=" "
    android:ems="10"
    android:id="@+id/editText6"
    android:layout_alignTop="@+id/textView8"
    android:layout_alignStart="@+id/editText5" />

    <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:ems="10"
    android:id="@+id/editText5"
    android:layout_alignTop="@+id/textView7"
    android:layout_alignStart="@+id/editText3" />
   </RelativeLayout>

1 个答案:

答案 0 :(得分:2)

你的创建表不正确。空格不正确,应该是:

db.execSQL("CREATE TABLE "+table_name+" (ID INTEGER PRIMARY KEY AUTOINCREMENT,NAME TEXT,PRICE TEXT,DATE TEXT,CAPACITY TEXT,PROMOCODE TEXT)");

还有下拉表中的空格:

db.execSQL("DROP TABLE IF EXISTS "+ table_name);