2015-09-08 74 views
0

我試圖在Android中使用SQLite開始,但是我有一些問題..
我從2012年編寫的教程中獲取代碼,但它現在不適合我,並向我顯示此錯誤:E/SQLiteLog:(1)在「Table」附近:語法錯誤

E/SQLiteLog﹕ (1) near "Table": syntax error

問題是與創建/打開數據庫。

package db.com.example.kids1.databasetest; 

import android.app.ListActivity; 
import android.database.Cursor; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.ListView; 
import android.widget.TextView; 

import org.w3c.dom.Text; 

import java.io.IOException; 
import java.util.ArrayList; 

public class MainActivity extends ListActivity{ 

    private final String DB_NAME = "Database"; 
    private final String TABLE_NAME = "Table"; 
    SQLiteDatabase DB = null; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     ArrayList<String> results = new ArrayList<>(); 

     String[] res = {"Red", "Green", "Text"}; 



     try { 
      DB = this.openOrCreateDatabase(DB_NAME, MODE_PRIVATE, null); 
      DB.execSQL("CREATE TABLE IF NOT EXISTS " + 
       TABLE_NAME + 
       "(Name VARCHAR, Street VARCHAR, Block INT, City VARCHAR, Tel VARCHAR);"); 
      mFillDbsTable(); 

      Cursor c = DB.rawQuery("SELECT Name, Street, Block, City, Tel FROM " + 
        TABLE_NAME + 
        " where Blcok == 9 LIMIT 5", null); 
      if (c!=null){ 

       if (c.moveToFirst()) { 
        do { 

         String name = c.getString(c.getColumnIndex("Name")); 
         String street = c.getString(c.getColumnIndex("Street")); 
         int block = c.getInt(c.getColumnIndex("Block")); 
         String city = c.getString(c.getColumnIndex("City")); 
         String tel = c.getString(c.getColumnIndex("Tel")); 

         results.add(name + "," + street + "," + block + "," + city + "," + tel); 
        } while (c.moveToNext()); 
       } 
      } 

      ListView list = (ListView)findViewById(android.R.id.list); 


      ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, res); 

      list.setAdapter(adapter); 

     } catch (SQLiteException se){ 
      Log.e(getClass().getSimpleName(), "Create/Open Database Problem."); 
     } 

    } 

    private void mFillDbsTable(){ 

     try { 
      DB.execSQL("INSERT INTO " + 
        TABLE_NAME + 
        " Values('Noam', 'Shkolnik', 9, 'Rehovot', '054-4900807');"); 

      DB.execSQL("INSERT INTO " + 
        TABLE_NAME + 
        " Values('Eyal', 'Shkolnik', 9, 'Rehovot', '055-4488779');"); 

      DB.execSQL("INSERT INTO " + 
        TABLE_NAME + 
        " Values('Yehontan', 'Shkolnik', 9, 'Rehovot', '058-7789547');"); 
     } catch (SQLiteException se) { 
      Log.e(getClass().getSimpleName(), "Could not create records."); 
     } 

    } 
} 
+0

嘗試:'int int = 5;'in java – Selvin

回答

1
private final String TABLE_NAME = "Table" 

不能創建一個名爲Table表,因爲它是一個保留關鍵字。
您最好創建一個名爲MyTable(或_Table或更好給出一個更健談的名稱,如Persons - 請注意複數s)表。

所以:

private final String TABLE_NAME = "MyTable" 

供您參考:https://sqlite.org/lang_keywords.html


可能(但不推薦)使用保留關鍵字,但你必須每次你是指使用特殊的分隔符到那張桌子。

喜歡的東西

private final String TABLE_NAME = "[Table]" 

而且也有在查詢另一個(雙)錯誤:

" where Blcok == 9 LIMIT 5" 

應該

" where Block = 9 LIMIT 5" 
+0

非常感謝!我不明白爲什麼它不起作用,它是完美的! –

0

嘗試VARCHAR(100)和刪除後;

0

你有幾個錯誤。首先,不要命名你的表Table。表是保留字(see docs),不能直接使用。它基本上不建議使用保留的話,那麼我建議你改變你的表名,但如果你堅持使用它,你需要首先引用它:BTW

If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite:

'keyword' A keyword in single quotes is a string literal.

"keyword" A keyword in double-quotes is an identifier.

[keyword] A keyword enclosed in square brackets is an identifier. This is not standard SQL. This quoting mechanism is used by MS Access and SQL Server and is included in SQLite for compatibility.

keyword A keyword enclosed in grave accents (ASCII code 96) is an identifier. This is not standard SQL. This quoting mechanism is used by MySQL and is included in SQLite for compatibility.

:你進一步select查詢將失敗也因以排除在哪裏:

where Blcok == 9 
+0

感謝您的幫助! –

相關問題