语法错误sqlite

时间:2017-02-06 17:23:44

标签: android sqlite android-sqlite

我正在尝试创建一个包含两个表的数据库。但是,每当我尝试运行它时,这种语法错误就会出现。我经过多次检查,但仍然无法弄清楚是什么问题。任何帮助将不胜感激!

public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
private static final String DATABASE_NAME = "contacts.db";
private static final String TABLE_NAME = "contacts";
private static final String COLUMN_ID = "id";
private static final String COL_1 = "Name";
private static final String COL_2 = "Pass";
private static final String COL_3 = "email";
private static final String COLUMN_ID1 = "id";

private static final String TABLE_NAME2 = "staff";
private static final String COLUMN_NAME = "Staff Name";
private static final String COLUMN_EMAIL = "Staff Email";
private static final String COLUMN_PASS = "Staff Password";
SQLiteDatabase db;

private static final String TABLE_CREATE = "create table contacts (id integer primary key not null , " +
        "name text not null , pass text not null, email text not null);";

private static final String TABLE_CREATE2 = "create table staff (id integer primary key not null , " +
        "name text not null , password text not null, email text not null);";


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

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    db.execSQL(TABLE_CREATE2);
    this.db = db;

}

public void insertContact(Contact c)
{
    db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    String query = "select * from contacts";
    Cursor cursor = db.rawQuery(query , null);
    int count = cursor.getCount();

    values.put(COLUMN_ID , count);
    values.put(COL_1 , c.getName());
    values.put(COL_2 , c.getPass());
    values.put(COL_3, c.getEmail());

    db.insert(TABLE_NAME, null, values);
    db.close();
}

public String searchPass(String name)
{
    db = this.getReadableDatabase();
    String query = "select Name, Pass from " + TABLE_NAME;
    Cursor cursor = db.rawQuery(query , null);
    String a, b;
    b = "not found";

    if(cursor.moveToFirst())
    {
        do {
            a = cursor.getString(0);


            if(a.equals(name))
            {
                b = cursor.getString(1);
                break;
            }
        }
        while (cursor.moveToNext());
    }

    return b;

}

public void insertStaff(Staff s)
{
    db = this.getWritableDatabase();
    ContentValues values1 = new ContentValues();

    String query1 = "select * from staff";
    Cursor cursor1 = db.rawQuery(query1 , null);
    int count = cursor1.getCount();

    values1.put(COLUMN_ID1 , count);
    values1.put(COLUMN_NAME , s.getStaffname());
    values1.put(COLUMN_EMAIL , s.getStaffemail());
    values1.put(COLUMN_PASS , s.getStaffpass());

    db.insert(TABLE_NAME2 , null , values1);
    db.close();

}

public String searchStaff(String name)
{
    db = this.getReadableDatabase();
    String query1 = "select name, password from " + TABLE_NAME2;
    Cursor cursor1 = db.rawQuery(query1, null);
    String a, b;
    b = "not found";
    if (cursor1.moveToFirst())
    {
        do{
            a=cursor1.getString(0);
            b=cursor1.getString(1);

            if (a.equals(name))
            {
                b = cursor1.getString(1);
                break;
            }

        }
        while(cursor1.moveToNext());
    }

    return b;
}




@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
    db.execSQL(query);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME2);
    this.onCreate(db);






}
}

logcat的

  

02-06 17:13:32.552 9101-9101 /? E / SQLiteLog:(1)接近“密码”:语法错误   02-06 17:13:32.552 9101-9101 /? E / SQLiteDatabase:插入id = 0时出错员工密码=员工电子邮件=员工姓名=       android.database.sqlite.SQLiteException:near“Password”:语法错误(代码1):,编译时:INSERT INTO员工(id,员工密码,员工电子邮件,员工姓名)VALUES(?,?,?,?)               在android.database.sqlite.SQLiteConnection.nativePrepareStatement(本机方法)               在android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)               在android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)               在android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)               在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:58)               在android.database.sqlite.SQLiteStatement。(SQLiteStatement.java:31)               在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)               在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)               在com.example.username.mp.DatabaseHelper.insertStaff(DatabaseHelper.java:106)               在com.example.username.mp.Signup.onRegClick(Signup.java:47)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在android.support.v7.internal.app.AppCompatViewInflater $ DeclaredOnClickListener.onClick(AppCompatViewInflater.java:273)               在android.view.View.performClick(View.java:4084)               在android.view.View $ PerformClick.run(View.java:16966)               在android.os.Handler.handleCallback(Handler.java:615)               在android.os.Handler.dispatchMessage(Handler.java:92)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:4745)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)               在dalvik.system.NativeStart.main(本地方法)

Signup.java

public class Signup extends AppCompatActivity {

DatabaseHelper helper1 = new DatabaseHelper(this);

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

public void onRegClick(View v)
{
    if(v.getId()== R.id.button20)
    {
        EditText name1 = (EditText)findViewById(R.id.editText12);
        EditText email1 = (EditText)findViewById(R.id.editText13);
        EditText pass3 = (EditText)findViewById(R.id.editText14);
        EditText pass4 = (EditText)findViewById(R.id.editText15);

        String name1str = name1.getText().toString();
        String email1str = email1.getText().toString();
        String pass3str = pass3.getText().toString();
        String pass4str = pass4.getText().toString();

        if(!pass3str.equals(pass4str))
        {
            Toast pop = Toast.makeText(Signup.this , "Password don't match" , Toast.LENGTH_SHORT);
            pop.show();
        }
        else
        {
            Staff s = new Staff();
            s.setStaffname(name1str);
            s.setStaffemail(email1str);
            s.setStaffpass(pass3str);

            helper1.insertStaff(s);
            Toast.makeText(getApplicationContext(), "Account Successfully Created ", Toast.LENGTH_LONG).show();
        }
    }

}

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

1 个答案:

答案 0 :(得分:1)

尝试从COLUMN_NAMECOLUMN_EMAILCOLUMN_PASS值中删除空格。