我正在尝试向数据库添加值,但我不断收到以下消息。数据库中有两个表,两个表在创建时都给出了相同的错误消息
" android.database.sqlite.SQLiteException:near" TABLEAssetstab&#34 ;:语法错误(代码1):,同时编译:CREATE TABLEAssetstab(Assetid TEXT NOT NULL,assetname TEXT NOT NULL,category TEXT NOT NULL,Purchasedate TEXT NOT NULL,assetstatus TEXT NOT NULL,warrenty TEXT NOT NULL);"
数据库Handeler类
public class DBHandler extends SQLiteOpenHelper {
// Database Name
public static final String DATABASE_NAME ="AssetDB.db";
//Database version
public static final int Databasevr = 1;
//Custodian Table Name
public static final String TABLE_CUSTODIAN = "Custodian";
// Columbs in the Custodian Table
public static final String CUSTODIAN_ID = "CustID";
public static final String CUSTODIAN_NAME = "CustName";
public static final String CUSTODIAN_DESIGNATION = "CustDesign";
public static final String CUSTODIAN_DEPARTMENT = "CustDepart";
// Creating the Assets Table
public static final String ASSETTAB ="Assetstab";
//Columbs in the Asset Table
public static final String ASSET_ID = "Assetid";
public static final String ASSET_Name="assetname";
public static final String ASSET_Category ="category";
public static final String ASSET_PurchaseDate = "Purchasedate";
public static final String ASSET_STATUS ="assetstatus";
public static final String ASSET_Warrenty ="warrenty";
// Creating a Custodian table and storing it in CREATE_TABLE_CUSTODIAN String
private static final String CREATE_TABLE_CUSTODIAN = "CREATE TABLE " + TABLE_CUSTODIAN + "("
+ CUSTODIAN_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,"
+ CUSTODIAN_NAME + " TEXT NOT NULL,"
+ CUSTODIAN_DESIGNATION + " TEXT NOT NULL,"
+ CUSTODIAN_DEPARTMENT + " TEXT NOT NULL" + ");";
// Creating a Assets table and storing it in CREATE_TABLE_CUSTODIAN String
private static final String CREATE_TABLE_ASSET ="CREATE TABLE " + ASSETTAB + "("
+ ASSET_ID + " TEXT NOT NULL,"
+ ASSET_Name + " TEXT NOT NULL,"
+ ASSET_Category +" TEXT NOT NULL,"
+ ASSET_PurchaseDate + " TEXT NOT NULL,"
+ ASSET_STATUS +" TEXT NOT NULL,"
+ ASSET_Warrenty +" TEXT NOT NULL"+ ");";
public void InsertCusto(Custodians cust)
{
// writing values to the Database
SQLiteDatabase db_database = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(CUSTODIAN_NAME,cust.getCust_Name());
values.put(CUSTODIAN_DESIGNATION,cust.getCust_Design());
values.put(CUSTODIAN_DEPARTMENT,cust.getDepartment());
//Inserting it to the Database
db_database.insert(TABLE_CUSTODIAN,null,values);
db_database.close();
}
}
和主类
public class AddCustodianActivity extends Activity implements OnClickListener {
public static final String TAG ="Adding Custodian";
private EditText CustNam,Design,Department;
List<Custodians> custodians = new ArrayList<Custodians>();
private Button SaveCust;
DBHandler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_custodian);
CustView();
handler = new DBHandler(getApplicationContext());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.add_custodian, 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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private void CustView()
{
this.CustNam = (EditText) findViewById(R.id.Custodianname);
this.Design = (EditText) findViewById(R.id.Design);
this.Department = (EditText) findViewById(R.id.Department);
this.SaveCust = (Button) findViewById(R.id.savecust);
this.SaveCust.setOnClickListener(this);
}
public void onClick(View v)
{
if(CustNam.getText().toString().length()== 0 && Design.getText().toString().length() == 0 && Department.getText().toString().length() == 0)
{
CustNam.setError("Please enter your Firs tName");
Design.setError("Please Enter The users Designation");
Department.setError("Please Enter the users Deprtmenmt");
}
else
{
Custodians custodians1 = new Custodians(handler.getCustodiancount(),String.valueOf(CustNam.getText()),String.valueOf(Design.getText()),String.valueOf(Department.getText()));
handler.InsertCusto(custodians1);
custodians.add(custodians1);
//adding to the list//custodians.add( new Custodians(0,CustNam.getText().toString(),Design.getText().toString(),Department.getText().toString()));
Toast.makeText(getApplicationContext(), CustNam.getText().toString() +"The Values are added",Toast.LENGTH_LONG).show();
}
}}
Logcat
05-20 18:02:12.126 10484-10484/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
05-20 18:02:12.126 10484-10484/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority [1], There is no sepolicy version file.
05-20 18:02:12.126 10484-10484/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=SEPF_GT-I9500_4.4.2_0024
05-20 18:02:12.126 10484-10484/com.example.nfcams I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
05-20 18:02:12.126 10484-10484/com.example.nfcams E/dalvikvm﹕ >>>>> Normal User
05-20 18:02:12.126 10484-10484/com.example.nfcams E/dalvikvm﹕ >>>>> com.example.nfcams [ userId:0 | appId:10266 ]
05-20 18:02:12.131 10484-10484/com.example.nfcams D/dalvikvm﹕ Late-enabling CheckJNI
05-20 18:02:12.251 10484-10484/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:02:12.251 10484-10484/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:02:12.366 10484-10484/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 55K, 9% free 16966K/18560K, paused 15ms, total 16ms
05-20 18:02:12.431 10484-10484/com.example.nfcams I/dalvikvm-heap﹕ Grow heap (frag case) to 39.163MB for 22118416-byte allocation
05-20 18:02:12.441 10484-10493/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 4% free 38565K/40164K, paused 11ms, total 11ms
05-20 18:02:12.561 10484-10484/com.example.nfcams D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 18:02:13.596 10484-10484/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:02:13.596 10484-10484/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:02:19.981 10484-10484/com.example.nfcams E/SQLiteLog﹕ (1) near "TABLECustodian": syntax error
05-20 18:02:19.981 10484-10484/com.example.nfcams D/AndroidRuntime﹕ Shutting down VM
05-20 18:02:19.981 10484-10484/com.example.nfcams W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4186ec08)
05-20 18:02:19.986 10484-10484/com.example.nfcams E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nfcams, PID: 10484
android.database.sqlite.SQLiteException: near "TABLECustodian": syntax error (code 1): , while compiling: CREATE TABLECustodian(CustID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,CustName TEXT NOT NULL,CustDesign TEXT NOT NULL,CustDepart TEXT NOT NULL);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1788)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1719)
at com.example.nfcams.DBHandler.onCreate(DBHandler.java:75)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.example.nfcams.DBHandler.getCustodiancount(DBHandler.java:142)
at com.example.nfcams.AddCustodianActivity.onClick(AddCustodianActivity.java:89)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
05-20 18:03:18.091 10484-10484/com.example.nfcams I/Process﹕ Sending signal. PID: 10484 SIG: 9
05-20 18:03:18.416 10587-10587/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:03:18.416 10587-10587/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:03:18.586 10587-10587/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 58K, 9% free 16966K/18564K, paused 14ms, total 14ms
05-20 18:03:18.621 10587-10587/com.example.nfcams I/dalvikvm-heap﹕ Grow heap (frag case) to 39.163MB for 22118416-byte allocation
05-20 18:03:18.631 10587-10596/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 4% free 38565K/40168K, paused 13ms, total 13ms
05-20 18:03:18.746 10587-10587/com.example.nfcams D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 18:03:19.621 10587-10587/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:03:19.621 10587-10587/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:03:20.061 10587-10587/com.example.nfcams W/ViewRootImpl﹕ Dropping event due to no window focus
05-20 18:14:36.886 13343-13343/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
05-20 18:14:36.886 13343-13343/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority [1], There is no sepolicy version file.
05-20 18:14:36.886 13343-13343/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=SEPF_GT-I9500_4.4.2_0024
05-20 18:14:36.886 13343-13343/com.example.nfcams I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
05-20 18:14:36.891 13343-13343/com.example.nfcams E/dalvikvm﹕ >>>>> Normal User
05-20 18:14:36.891 13343-13343/com.example.nfcams E/dalvikvm﹕ >>>>> com.example.nfcams [ userId:0 | appId:10266 ]
05-20 18:14:36.891 13343-13343/com.example.nfcams D/dalvikvm﹕ Late-enabling CheckJNI
05-20 18:14:36.976 13343-13343/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:14:36.981 13343-13343/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:14:37.136 13343-13343/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 84K, 9% free 16966K/18592K, paused 21ms, total 21ms
05-20 18:14:37.181 13343-13343/com.example.nfcams I/dalvikvm-heap﹕ Grow heap (frag case) to 39.163MB for 22118416-byte allocation
05-20 18:14:37.191 13343-13352/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 5% free 38564K/40196K, paused 14ms, total 14ms
05-20 18:14:37.326 13343-13343/com.example.nfcams D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 18:15:36.376 13484-13484/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority [0], There is no sepolicy file.
05-20 18:15:36.376 13484-13484/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority [1], There is no sepolicy version file.
05-20 18:15:36.376 13484-13484/com.example.nfcams I/SELinux﹕ Function: selinux_android_load_priority , priority version is VE=SEPF_GT-I9500_4.4.2_0024
05-20 18:15:36.376 13484-13484/com.example.nfcams I/SELinux﹕ selinux_android_seapp_context_reload: seapp_contexts file is loaded from /seapp_contexts
05-20 18:15:36.376 13484-13484/com.example.nfcams E/dalvikvm﹕ >>>>> Normal User
05-20 18:15:36.376 13484-13484/com.example.nfcams E/dalvikvm﹕ >>>>> com.example.nfcams [ userId:0 | appId:10266 ]
05-20 18:15:36.381 13484-13484/com.example.nfcams D/dalvikvm﹕ Late-enabling CheckJNI
05-20 18:15:36.481 13484-13484/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:15:36.481 13484-13484/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:15:36.636 13484-13484/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 69K, 9% free 16966K/18576K, paused 16ms, total 16ms
05-20 18:15:36.686 13484-13484/com.example.nfcams I/dalvikvm-heap﹕ Grow heap (frag case) to 39.163MB for 22118416-byte allocation
05-20 18:15:36.706 13484-13493/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 5% free 38565K/40180K, paused 17ms, total 17ms
05-20 18:15:36.946 13484-13484/com.example.nfcams D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 18:16:13.856 14437-14437/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:16:13.856 14437-14437/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:16:13.981 14437-14437/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 61K, 9% free 16966K/18568K, paused 15ms, total 15ms
05-20 18:16:14.026 14437-14437/com.example.nfcams I/dalvikvm-heap﹕ Grow heap (frag case) to 39.163MB for 22118416-byte allocation
05-20 18:16:14.036 14437-14446/com.example.nfcams D/dalvikvm﹕ GC_FOR_ALLOC freed 1K, 5% free 38564K/40172K, paused 14ms, total 14ms
05-20 18:16:14.176 14437-14437/com.example.nfcams D/OpenGLRenderer﹕ Enabling debug mode 0
05-20 18:16:45.761 14437-14437/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:16:45.766 14437-14437/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:16:50.656 14437-14437/com.example.nfcams W/ApplicationPackageManager﹕ getCSCPackageItemText()
05-20 18:16:50.656 14437-14437/com.example.nfcams I/PersonaManager﹕ getPersonaService() name persona_policy
05-20 18:16:57.811 14437-14437/com.example.nfcams E/SQLiteLog﹕ (1) near "TABLEAssetstab": syntax error
05-20 18:16:57.816 14437-14437/com.example.nfcams D/AndroidRuntime﹕ Shutting down VM
05-20 18:16:57.816 14437-14437/com.example.nfcams W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4186ec08)
05-20 18:16:57.826 14437-14437/com.example.nfcams E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.nfcams, PID: 14437
android.database.sqlite.SQLiteException: near "TABLEAssetstab": syntax error (code 1): , while compiling: CREATE TABLEAssetstab(Assetid TEXT NOT NULL,assetname TEXT NOT NULL,category TEXT NOT NULL,Purchasedate TEXT NOT NULL,assetstatus TEXT NOT NULL,warrenty TEXT NOT NULL);
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1788)
at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1719)
at com.example.nfcams.DBHandler.onCreate(DBHandler.java:76)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
at com.example.nfcams.DBHandler.getCustodiancount(DBHandler.java:142)
at com.example.nfcams.AddCustodianActivity.onClick(AddCustodianActivity.java:89)
at android.view.View.performClick(View.java:4633)
at android.view.View$PerformClick.run(View.java:19330)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
将其更改为此。
private static final String CREATE_TABLE_CUSTODIAN = "CREATE TABLE " + TABLE_CUSTODIAN + " ("
+ CUSTODIAN_ID + " INTEGER PRIMARY KEY,"
+ CUSTODIAN_NAME + " TEXT NOT NULL,"
+ CUSTODIAN_DESIGNATION + " TEXT NOT NULL,"
+ CUSTODIAN_DEPARTMENT + " TEXT NOT NULL);";
我的数据库是如何创建的
public static final String DATABASE_TABLE_ALL = "ALL_TABLE";
public static final String KEY_ROWID = "_id";
public static final String APP_ID = "app_id";
public static final String APP_NAME = "app_name";
public static final String APP_IMAGE_NAME = "app_image_name";
public static final String APP_DESCRIPTION = "app_description";
public static final String IOS_LOCAL_LINK = "app_ios_local_link";
public static final String IOS_STORE_LINK = "app_ios_store_link";
public static final String IOS_STORE_ID = "app_ios_store_id";
public static final String ANDROID_LOCAL_LINK = "app_android_local_link";
public static final String ANDROID_STORE_LINK = "app_android_store_link";
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE_ALL + " ("
+ KEY_ROWID + " INTEGER PRIMARY KEY, " + APP_ID
+ " TEXT NOT NULL, " + APP_NAME + " TEXT NOT NULL, "
+ APP_IMAGE_NAME + " TEXT NOT NULL, "
+ APP_DESCRIPTION + " TEXT NOT NULL, "
+ IOS_LOCAL_LINK + " TEXT NOT NULL, "
+ IOS_STORE_LINK + " TEXT NOT NULL, " + IOS_STORE_ID
+ " TEXT NOT NULL, " + ANDROID_LOCAL_LINK
+ " TEXT NOT NULL, " + ANDROID_STORE_LINK
+ " TEXT NOT NULL);");
}