2016-08-18 67 views
0

SQLite的外鍵支持下面這個教程http://www.sqlite.org/foreignkeys.html我需要3個表數據庫但我不能修復一個關於「SQL邏輯錯誤....近洋」啓用VB的淨

Private Sub CreateDataBase() 

    Dim conn = New SQLiteConnection("Data Source=MyDataBase.sqlite;Version=3") 

    Try 
     Using (conn) 
      conn.Open() 


      '3 Tables I need to create 
      Dim mainTable = "CREATE TABLE IF NOT EXISTS users (userID INTEGER PRIMARY KEY, name VARCHAR(20))" 
      Dim tableA = "CREATE TABLE IF NOT EXISTS tableA (ItemA VARCHAR(20), user INTEGER) FOREIGN KEY(user) REFERENCES users (userID)" 
      Dim tableB = "CREATE TABLE IF NOT EXISTS tableB (ItemB VARCHAR(20), user INTEGER) FOREIGN KEY(user) REFERENCES users (userID)" 

      Dim cmdConexion As SQLiteCommand = New SQLiteCommand(mainTable, conn) 


    'Try for the mainTable 
      Try 
       cmdConexion.ExecuteNonQuery() 
      Catch ex As Exception 
       MsgBox(ex.ToString()) 
      End Try 


    'Set and Try for tableA 
      cmdConexion.CommandText = tableA 
      Try 
       cmdConexion.ExecuteNonQuery() 
      Catch ex As Exception 
       MsgBox(ex.ToString()) 
      End Try 


    'Set and Try for tableB 
      cmdConexion.CommandText = tableB 

      Try 
       cmdConexion.ExecuteNonQuery() 
       Catch ex As Exception 
       MsgBox(ex.ToString()) 
      End Try 

     End Using 

    Catch ex As Exception 
     MsgBox(ex.ToString()) 
    End Try 

End Sub 
消息

相同的代碼只適用於mainTable。

我用一條線tryed像

Pragma("foreign_keys") = 1 

甚至

cmdConexion.CommandText = "PRAGMA foreign_keys = ON" 

但還是我不能修復錯誤。

SQL行或Pragma上的錯誤在哪裏?

注意: 另外,是否有另一種方法來執行相同的TryCatch的SQL創建表或更好的嘗試 - 捕獲每個表?

回答

2

在CREATE TABLE語句中有一個語法錯誤int。該FOREIGN KEY雙組分必須在括號

CREATE TABLE IF NOT EXISTS tableA (
    ItemA VARCHAR(20), 
    user INTEGER, 
    FOREIGN KEY(user) REFERENCES users (userID) 
) 

或者乾脆

CREATE TABLE IF NOT EXISTS tableA (
    ItemA VARCHAR(20), 
    user INTEGER REFERENCES users (userID) 
) 

注中:​​像一個犯錯「......近一些」往往只是一個語法錯誤。

+0

對!這兩個解決方案都有效我的錯誤是一個「)」不合適。感謝您的提示 – fedeteka