语法错误(代码1):,编译时:

时间:2016-03-01 22:38:41

标签: java android sqlite

我收到以下错误

android.database.sqlite.SQLiteException: near "TEXT": syntax error (code 1): , while compiling: CREATE TABLE contacts (name TEXT, phone INTEGER, email TEXT

生成错误的特定代码行是

sqLiteDatabase.execSQL("CREATE TABLE contacts (name TEXT, phone INTEGER, email TEXT");

以下是完整代码:

package ca.truewebdev.sqlitetest1;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SQLiteDatabase sqLiteDatabase = getBaseContext().openOrCreateDatabase("sqlite-test-1.db", MODE_PRIVATE, null);
    sqLiteDatabase.execSQL("CREATE TABLE contacts (name TEXT, phone INTEGER,email TEXT");
    sqLiteDatabase.execSQL("INSERT INTO contacts VALUES('tim',6456789,'tim@email/com'");
    sqLiteDatabase.execSQL("INSERT INTO contacts VALUES('chris',324243,'chris@email/com'");

    //set up to be able to access data
    Cursor query = sqLiteDatabase.rawQuery("SELECT * FROM contacts", null);

    //check to see if any data, moves to first record or returns false if none
    if (query.moveToFirst()) {
        // cycle through all records
        String name = query.getString(0);
        int phone = query.getInt(1);
        String email = query.getString(2);
        Toast.makeText(getBaseContext(), "Name = " + name + " phone = " + phone +
                " email = " + email, Toast.LENGTH_LONG).show();

    }else{
        Toast.makeText(getBaseContext(), "Error retrieving data", Toast.LENGTH_LONG).show();
    }

}
}

为什么我收到此错误?我正在关注一个教程。我非常确定我输入的内容是正确的。

2 个答案:

答案 0 :(得分:1)

更改此行

sqLiteDatabase.execSQL("CREATE TABLE contacts (name TEXT, phone INTEGER, email TEXT");

sqLiteDatabase.execSQL("CREATE TABLE contacts (name TEXT, phone INTEGER, email TEXT)");

答案 1 :(得分:0)

在"发送电子邮件文本"之后,您缺少一个右括号。尝试:

sqLiteDatabase.execSQL(" CREATE TABLE联系人(姓名TEXT,手机INTEGER,电子邮件TEXT)");