2017-02-06 12 views
0

私は2つのテーブルを持つデータベースを作成しようとしています。しかし、この構文エラーは、私がそれを実行しようとするたびに今後も続きます。私は何度もチェックしましたが、何が間違っているのかまだ分かりません。どんな助けもありがとう!構文error 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) "Password"の近く:構文エラー 02-06 17:13:32.552 9101-9101 /? E/SQLiteDatabase:id = 0を挿入しているエラーStaff Password =スタッフEmail =スタッフ名= android.database.sqlite.SQLiteException: "パスワード"の近く:構文エラー(コード1):、コンパイル中:INSERT INTO staff(id、Staffパスワード、スタッフの電子メール、スタッフ名)VALUES(?、?、?、?) android.database.sqlite.SQLiteConnection.nativePrepareStatement(ネイティブメソッド) android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882 android.database.sqlite.SQLiteProgramでandroid.database.sqlite.SQLiteConnection.prepare android.database.sqlite.SQLiteSession.prepareで(SQLiteConnection.java:493) (SQLiteSession.java:588) で) 。(SQLiteProgram。 (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) at com.example.username.mp.Signup.onRegClick(Signup.java:47) at java.lang.reflect.Method.invokeNative(ネイティブメソッド) at 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) at android.view.View $ PerformClick.run(View.java:16966) android.os.Handler.handleCallback(Handler.j (ActivityThread.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(ネイティブメソッド) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run dalvik.system.NativeStart.main(ネイティブメソッド)

Signup.javaでcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)で(ZygoteInit.java:786)

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

COLUMN_NAME,COLUMN_EMAILおよびCOLUMN_PASSの値を削除してください。

+0

お返事ありがとうございました。 COLUMN_NAME = "Staff Name"という意味のスペースを削除することですか? ? –

+0

はい、まさに:) –

+0

残念ながら、まったく同じエラーがまだポップアップしています。( –