Android- SQLiteException:(近表:语法错误)

时间:2021-03-18 23:07:04

Hello I am trying to create an sqlite for my app, but its not working. In the registration activity I am trying to save the info to the database.

你好,我正在为我的应用创建一个sqlite,但是它不能工作。在注册活动中,我试图将信息保存到数据库中。

05-24 14:47:37.047: E/SQLiteLog(18202): (1) near "TABLEMembers": syntax error
05-24 14:47:37.047: D/AndroidRuntime(18202): Shutting down VM
05-24 14:47:37.047: W/dalvikvm(18202): threadid=1: thread exiting with uncaught    exception (group=0x41e1b8b0)
05-24 14:47:37.057: E/AndroidRuntime(18202): FATAL EXCEPTION: main
05-24 14:47:37.057: E/AndroidRuntime(18202): android.database.sqlite.SQLiteException: near "TABLEMembers": syntax error (code 1): , while compiling: CREATE TABLEMembers(EmployerBOOLEAN,UnEmploydBOOLEAN,FirstNameTEXT,LastNameTEXT,NumberTEXT,EmailTEXT PRIMARY KEY,CompanyNameTEXT,PasswordTEXT)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1682)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1611)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at com.example.jobfinder.DatabaseHelper.onCreate(DatabaseHelper.java:53)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at com.example.jobfinder.DatabaseHelper.InsertRegestration(DatabaseHelper.java:71)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at com.example.jobfinder.RegestrationActivity$3.onClick(RegestrationActivity.java:108)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.view.View.performClick(View.java:4421)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.view.View$PerformClick.run(View.java:17903)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.os.Handler.handleCallback(Handler.java:730)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.os.Looper.loop(Looper.java:213)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at android.app.ActivityThread.main(ActivityThread.java:5225)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at java.lang.reflect.Method.invokeNative(Native Method)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at java.lang.reflect.Method.invoke(Method.java:525)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
05-24 14:47:37.057: E/AndroidRuntime(18202):    at dalvik.system.NativeStart.main(Native Method)
05-24 14:47:38.308: I/Process(18202): Sending signal. PID: 18202 SIG: 9
05-24 14:47:38.698: D/dalvikvm(18352): GC_FOR_ALLOC freed 492K, 33% free 7535K/11208K, paused 13ms, total 13ms
05-24 14:47:38.718: I/dalvikvm-heap(18352): Grow heap (frag case) to 17.131MB for 9216016-byte allocation
05-24 14:47:38.879: D/dalvikvm(18352): GC_FOR_ALLOC freed 4154K, 37% free 12891K/20212K, paused 12ms, total 12ms
05-24 14:47:38.919: D/libEGL(18352): loaded /vendor/lib/egl/libEGL_adreno.so
05-24 14:47:38.919: D/libEGL(18352): loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
05-24 14:47:38.929: D/libEGL(18352): loaded /vendor/lib/egl/libGLESv2_adreno.so
05-24 14:47:38.929: I/Adreno-EGL(18352): <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build:  (CL4169980)
05-24 14:47:38.929: I/Adreno-EGL(18352): OpenGL ES Shader Compiler Version: 17.01.10.SPL
05-24 14:47:38.929: I/Adreno-EGL(18352): Build Date: 12/01/13 Sun
05-24 14:47:38.929: I/Adreno-EGL(18352): Local Branch: 
05-24 14:47:38.929: I/Adreno-EGL(18352): Remote Branch: 
05-24 14:47:38.929: I/Adreno-EGL(18352): Local Patches: 
05-24 14:47:38.929: I/Adreno-EGL(18352): Reconstruct Branch: 
05-24 14:47:38.969: D/OpenGLRenderer(18352): Enabling debug mode 0
05-24 14:47:40.140: W/IInputConnectionWrapper(18352): showStatusIcon on inactive       InputConnection

Here is my databaseHandler class:

这是我的databaseHandler类:

package com.example.jobfinder;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {

    // Logcat tag
    private static final String LOG = "DatabaseHelper";

    // Database Version
    private static final int DATABASE_VERSION = 1;

    // Database Name
    private static final String DATABASE_NAME = "JobFinder";

    // Table Names
    private static final String JobOffering = "JobOffering";
    private static final String Members = "Members";

    //Columes for memebers 
    private static final String Employer="Employer";
    private static final String UnEmployed="UnEmployd";
    private static final String FirstName="FirstName";
    private static final String LastName="LastName";
    private static final String Number="Number";
    private static final String Email="Email";
    private static final String CompanyName="CompanyName";
    private static final String Password="Password";

    //columes for JobOffering
    private static final String JobPosition="JobPosition";
    private static final String RequiredKnowladge="RequiredKnowladge";
    private static final String Credentials="Credentials";

    private static final String CREATE_Members="CREATE TABLE"+Members+"("+Employer+"BOOLEAN,"+UnEmployed+"BOOLEAN,"+FirstName+"TEXT,"+LastName+"TEXT,"
            +Number+"TEXT,"+Email+"TEXT PRIMARY KEY,"+CompanyName+"TEXT,"+Password+"TEXT"+")";

    private static final String CREATE_JobOffering="CRAETE TABLE"+JobOffering+"("+JobPosition+"TEXT,"+RequiredKnowladge+"TEXT,"+Credentials+"TEXT,"
    +Email+"TEXT PRIMARY KEY"+")";

    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(CREATE_Members);
        db.execSQL(CREATE_JobOffering);

    }

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

    }

    public void InsertRegestration(RegestrationClass regestration)
    {

        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(Employer,regestration.getEmployer());
        values.put(UnEmployed,regestration.getUnemployed());
        values.put(FirstName,regestration.getFname());
        values.put(LastName,regestration.getLname());
        values.put(Number,regestration.getNumber());
        values.put(Email,regestration.getEmail());
        values.put(CompanyName,regestration.getCName());
        values.put(Password,regestration.getPassword());
        db.insert(Members, null, values);

    }

    public void ReturnLogin(LoginClass login)
    {
        String selectQuery = "SELECT  Password,Email FROM " + Members + " WHERE "
                + Email + " = " +login.getUsername() ;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        login.setUsername(cursor.getString(cursor.getColumnIndex(Email)));
        login.setPassword(cursor.getString(cursor.getColumnIndex(Password)));
    }
}

In this class I use the EditText to get the value and give them to the database:

在这个类中,我使用EditText获取值,并将其提供给数据库:

package com.example.jobfinder;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;

public class RegestrationActivity extends Activity {

private Button mCreate;
private RadioGroup mRadioPositionGroup;



 private EditText Fname,Lname,Number,Email,Password,mCName;
 private RadioButton mUnemployed;
  private RadioButton mEmployer;
  DatabaseHelper db;

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

    db = new DatabaseHelper(getApplicationContext());

     mCreate=(Button)findViewById(R.id.create_button);
     mCName=(EditText)findViewById(R.id.companyname_edit);
    mRadioPositionGroup=(RadioGroup)findViewById(R.id.radiogroup);
     mUnemployed=(RadioButton)findViewById(R.id.unemployed_radio);
    mEmployer=(RadioButton)findViewById(R.id.employer_radio);
      Fname=(EditText)findViewById(R.id.fname_edit);
      Email=(EditText)findViewById(R.id.email_edit);
      Number=(EditText)findViewById(R.id.number_edit);
      Lname=(EditText)findViewById(R.id.lname_edit);
      Password=(EditText)findViewById(R.id.password_edit);

    mEmployer.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View V)
        {
            int selectedid=mRadioPositionGroup.getCheckedRadioButtonId();
            if(selectedid==(R.id.employer_radio))
            {mCName.setEnabled(true);
            mCName.setTextColor(Color.BLACK);
            }
        }

    });

    mUnemployed.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View V)
        {
            int selectedid=mRadioPositionGroup.getCheckedRadioButtonId();
            if(selectedid==(R.id.unemployed_radio))
            {mCName.setEnabled(false);
            mCName.setTextColor(Color.BLACK);
            }
        }
    });

    mCreate.setOnClickListener(new View.OnClickListener()
    { 
        @Override
        public void onClick(View v)
        {
            int selectedid=mRadioPositionGroup.getCheckedRadioButtonId();
            if(selectedid==(R.id.employer_radio))
            {
             //regestrationclass.setEmployer(true);
                RegestrationClass regestrationclass=new RegestrationClass(true,false,Fname.getText().toString(),Lname.getText().toString(),Email.getText().toString(),Number.getText().toString(),mCName.getText().toString(),Password.getText().toString());
                db.InsertRegestration(regestrationclass);
            }
            else{

                // regestrationclass.setUnemployed(true);
                RegestrationClass regestrationclass=new RegestrationClass(false,true,Fname.getText().toString(),Lname.getText().toString(),Email.getText().toString(),Number.getText().toString(),null,Password.getText().toString());
                db.InsertRegestration(regestrationclass);
            }
            finish();
        }
    });
}}

1 个解决方案

#1


2  

These instructions are wrong (missing a space between the field names and their types, also between TABLE and table name - The second instruction has the CREATE command misspelled):

这些指令是错误的(在字段名和它们的类型之间,也在表和表名之间缺少空格——第二个指令的CREATE命令拼写错误):

private static final String CREATE_Members="CREATE TABLE"+Members+"("+Employer+"BOOLEAN,"+UnEmployed+"BOOLEAN,"+FirstName+"TEXT,"+LastName+"TEXT,"
        +Number+"TEXT,"+Email+"TEXT PRIMARY KEY,"+CompanyName+"TEXT,"+Password+"TEXT"+")";

private static final String CREATE_JobOffering="CRAETE TABLE"+JobOffering+"("+JobPosition+"TEXT,"+RequiredKnowladge+"TEXT,"+Credentials+"TEXT,"
+Email+"TEXT PRIMARY KEY"+")";

Should be:

应该是:

private static final String CREATE_Members="CREATE TABLE "+Members+"("+Employer+" BOOLEAN,"+UnEmployed+" BOOLEAN,"+FirstName+" TEXT,"+LastName+" TEXT,"
        +Number+" TEXT,"+Email+" TEXT PRIMARY KEY,"+CompanyName+" TEXT,"+Password+" TEXT"+")";

private static final String CREATE_JobOffering="CREATE TABLE "+JobOffering+"("+JobPosition+" TEXT,"+RequiredKnowladge+" TEXT,"+Credentials+" TEXT,"
+Email+" TEXT PRIMARY KEY"+")";

#1


2  

These instructions are wrong (missing a space between the field names and their types, also between TABLE and table name - The second instruction has the CREATE command misspelled):

这些指令是错误的(在字段名和它们的类型之间,也在表和表名之间缺少空格——第二个指令的CREATE命令拼写错误):

private static final String CREATE_Members="CREATE TABLE"+Members+"("+Employer+"BOOLEAN,"+UnEmployed+"BOOLEAN,"+FirstName+"TEXT,"+LastName+"TEXT,"
        +Number+"TEXT,"+Email+"TEXT PRIMARY KEY,"+CompanyName+"TEXT,"+Password+"TEXT"+")";

private static final String CREATE_JobOffering="CRAETE TABLE"+JobOffering+"("+JobPosition+"TEXT,"+RequiredKnowladge+"TEXT,"+Credentials+"TEXT,"
+Email+"TEXT PRIMARY KEY"+")";

Should be:

应该是:

private static final String CREATE_Members="CREATE TABLE "+Members+"("+Employer+" BOOLEAN,"+UnEmployed+" BOOLEAN,"+FirstName+" TEXT,"+LastName+" TEXT,"
        +Number+" TEXT,"+Email+" TEXT PRIMARY KEY,"+CompanyName+" TEXT,"+Password+" TEXT"+")";

private static final String CREATE_JobOffering="CREATE TABLE "+JobOffering+"("+JobPosition+" TEXT,"+RequiredKnowladge+" TEXT,"+Credentials+" TEXT,"
+Email+" TEXT PRIMARY KEY"+")";