创建数据库出错

时间:2015-04-06 22:55:53

标签: java android xml sqlite

我尝试创建一个提交按钮,用于保存用户可以在其上键入的三个编辑文本的结果。但出了点问题。我只有一个活动和三个Java类,分别是SignUp,TableData和DatabaseOperations 请看这里的代码: XML:    

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textPersonName"
    android:text="First,Last"
    android:ems="10"
    android:id="@+id/txtName"
    android:textSize="10dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView2"
    android:layout_toEndOf="@+id/textView2" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:inputType="textEmailAddress"
    android:ems="10"
    android:id="@+id/txtEmail"
    android:textSize="10dp"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@+id/textView3"
    android:layout_toEndOf="@+id/textView3" />

<EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/txtFood"
    android:textSize="8dp"
    android:layout_alignBottom="@+id/textView"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_alignLeft="@+id/txtEmail"
    android:layout_alignStart="@+id/txtEmail" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Favorite Restaurant"
    android:id="@+id/textView"
    android:textSize="10dp"
    android:textColor="#ff4713ff"
    android:layout_below="@+id/textView4"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="10dp" />

<!--suppress AndroidDomInspection -->

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/rg1"
    android:layout_below="@+id/textView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true">

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="McDonald&apos;s"
        android:id="@+id/radioButton"
        android:elegantTextHeight="false"
        android:state_checked="true"
        android:checked="false"
        android:button="@android:drawable/btn_radio"
        android:layout_below="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="10dp"
        android:layout_marginTop="0dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Burger King"
        android:id="@+id/radioButton2"
        android:elegantTextHeight="false"
        android:state_checked="true"
        android:checked="false"
        android:button="@android:drawable/btn_radio"
        android:layout_alignTop="@+id/radioButton"
        android:layout_toRightOf="@+id/txtFood"
        android:layout_toEndOf="@+id/txtFood"
        android:textSize="10dp"
        android:layout_marginTop="-15dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Wendy&apos;s"
        android:id="@+id/radioButton4"
        android:elegantTextHeight="false"
        android:state_checked="true"

        android:checked="false"
        android:button="@android:drawable/btn_radio"
        android:layout_alignBaseline="@+id/radioButton3"
        android:layout_alignBottom="@+id/radioButton3"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textSize="10dp"
        android:layout_marginTop="-15dp" />

    <RadioButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Papa John&apos;s Pizza"
        android:id="@+id/radioButton5"
        android:elegantTextHeight="false"
        android:state_checked="true"
        android:checked="false"
        android:button="@android:drawable/btn_radio"
        android:layout_below="@+id/radioButton4"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:textSize="10dp"
        android:layout_marginTop="-15dp" />

    <RadioButton
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Next"
        android:id="@+id/radioButton3"
        android:elegantTextHeight="false"
        android:state_checked="true"

        android:checked="false"
        android:button="@android:drawable/btn_radio"
        android:textSize="10dp"
        android:layout_below="@+id/rg1"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="-15dp" />

</RadioGroup>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Submit"
    android:id="@+id/button3"
    android:textSize="10dp"
    android:clickable="true"
    android:enabled="true"
    android:onClick="Submit"
    android:layout_below="@+id/rg1"
    android:layout_alignLeft="@+id/txtName"
    android:layout_alignStart="@+id/txtName" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Name"
    android:id="@+id/textView2"
    android:textSize="10dp"
    android:textColor="#ff4713ff"
    android:layout_alignBaseline="@+id/txtName"
    android:layout_alignBottom="@+id/txtName"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:paddingLeft="10dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email"
    android:id="@+id/textView3"
    android:textSize="10dp"
    android:textColor="#ff4713ff"
    android:layout_alignBaseline="@+id/txtName"
    android:layout_alignBottom="@+id/txtName"
    android:layout_alignLeft="@+id/button8"
    android:layout_alignStart="@+id/button8" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Favorite Food"
    android:id="@+id/textView4"
    android:textSize="10dp"
    android:textColor="#ff4713ff"
    android:layout_below="@+id/txtName"
    android:layout_toRightOf="@+id/button3"
    android:layout_toEndOf="@+id/button3" />

<CheckBox
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Email Me"
    android:id="@+id/checkBox"

    android:checked="false"
    android:layout_marginBottom="16dp"
    android:layout_alignBottom="@+id/rg1"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="19dp"
    android:layout_marginEnd="19dp"
    android:enabled="true"
    android:clickable="true" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Home"
    android:id="@+id/button8"
    android:textSize="10dp"
    android:clickable="true"
    android:enabled="true"
    android:onClick="HomePage"
    android:layout_below="@+id/rg1"
    android:layout_toRightOf="@+id/txtName"
    android:layout_toEndOf="@+id/txtName" />

Java代码:

package com.example.android.ch2;
import android.content.Context;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;


public class SignUp extends ActionBarActivity {
EditText nameTxt,emailTxt,foodTxt;
String user_name,user_email,user_food;
Button REG;
Context ctx = this;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_sign_up);
    nameTxt = (EditText) findViewById(R.id.txtName);
    emailTxt = (EditText) findViewById(R.id.txtEmail);
    foodTxt = (EditText) findViewById(R.id.txtFood);
    REG = (Button) findViewById(R.id.button3);
    REG.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            user_name = nameTxt.getText().toString();
            user_email = emailTxt.getText().toString();
            user_food = foodTxt.getText().toString();

      }

        DatabaseOperations DB = new DatabaseOperations(ctx);
        DB.putInformation(DB,user_name,user_email,user_food);

    });
   }




@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_sign_up, 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);

}
public void HomePage(View v){
    Intent intent=new Intent(v.getContext(),Login.class);
    startActivity(intent);

}}

表Jave:

package com.example.android.ch2;

import android.provider.BaseColumns;


public class TableData {
public TableData(){

}
public static abstract class TableInfo implements BaseColumns{
    public static final String USER_NAME = "user_name";
    public static final String USER_EMAIL = "user_email";
    public static final String USER_FOOD = "user_food";
    public static final String DATABASE_NAME = "user_info";
    public static final String TABLE_NAME = "reg_info";a
}
}

数据库Java

 package com.example.android.ch2;

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;
 import android.util.Log;


public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE"+ TableData.TableInfo.TABLE_NAME+"   ("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_EMAIL+"     TEXT,"+ TableData.TableInfo.USER_FOOD+" TEXT);";
public DatabaseOperations(Context context){
    super(context, TableData.TableInfo.DATABASE_NAME,null,database_version);
}

@Override
public void onCreate(SQLiteDatabase sdb) {
    sdb.execSQL(CREATE_QUERY);
    Log.d("Database Operations", "Table created");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}
public void putInformation (DatabaseOperations dop, String name,String email,String food){

SQLiteDatabase SQ = dop.getWritableDatabase();
    ContentValues cv = new ContentValues();
    cv.put(TableData.TableInfo.USER_NAME, name);
    cv.put(TableData.TableInfo.USER_EMAIL, email);
    cv.put(TableData.TableInfo.USER_FOOD, food);
    long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
    Log.d ("Database operations", "One raw inserted");

  }
}

LogCat片段:

-06 22:25:41.718 4004-4004/? E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: com.example.android.ch2,
 PID: 4004 android.database.sqlite.SQLiteException: near "TABLEreg_info": syntax error (code 1): , while compiling:
 CREATE TABLEreg_info(user_name TEXT,user_email TEXT,user_food TEXT); at android.database.sqlite.SQLiteConnection.nativePrepareStatement
(Native Method

1 个答案:

答案 0 :(得分:2)

您在TABLE和TableData.TableInfo.TABLE_NAME之间没有空格

使用此

public String CREATE_QUERY =&#34; CREATE TABLE&#34; + TableData.TableInfo.TABLE_NAME +&#34; (&#34; + TableData.TableInfo.USER_NAME +&#34; TEXT,&#34; + TableData.TableInfo.USER_EMAIL +&#34; TEXT,&#34; + TableData.TableInfo.USER_FOOD +&#34; TEXT);&#34 ;;

不是这个

public String CREATE_QUERY =&#34; CREATE TABLE&#34; + TableData.TableInfo.TABLE_NAME +&#34; (&#34; + TableData.TableInfo.USER_NAME +&#34; TEXT,&#34; + TableData.TableInfo.USER_EMAIL +&#34; TEXT,&#34; + TableData.TableInfo.USER_FOOD +&#34; TEXT);&#34 ;;

在字符串

中的 TABLE 后面看空格