我收到错误FATAL EXCEPTION:android.database.sqlite.SQLiteException:near“)”:语法错误(代码1?

时间:2018-03-08 23:14:44

标签: java android sqlite

我正在制作新闻应用并在我的代码中出现以下错误。

我的数据库中出现以下错误

FATAL EXCEPTION: main
    Process: com.edgar.yodgorbekkomilo.newsapp, PID: 1328
    android.database.sqlite.SQLiteException: near “)”: syntax error (code 1): , while compiling: CREATE TABLE IF NOT EXISTS article ( _id INTEGER PRIMARY KEY AUTOINCREMENT, title TEXT NOT NULL, title_description TEXT, author TEXT, );
    at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
    at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
    at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
    at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
    at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:58)
    at android.database.sqlite.SQLiteStatement.(SQLiteStatement.java:31)
    at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1677)
    at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1608)
    at com.edgar.yodgorbekkomilo.newsapp.ArticleSQLiteOpenHelper.onCreate(ArticleSQLiteOpenHelper.java:85)
    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:294)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:194)
    at com.edgar.yodgorbekkomilo.newsapp.provider.BaseContentProvider.insert(BaseContentProvider.java:81)
    at com.edgar.yodgorbekkomilo.newsapp.provider.ArticleProvider.insert(ArticleProvider.java:68)
    at android.content.ContentProvider$Transport.insert(ContentProvider.java:267)
    at android.content.ContentResolver.insert(ContentResolver.java:1539)
    at com.edgar.yodgorbekkomilo.newsapp.NewsDetailActivity$4.onClick(NewsDetailActivity.java:126)
    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

在我的数据库类

下面
public class ArticleSQLiteOpenHelper  extends SQLiteOpenHelper{

    public static final String DATABASE_FILE_NAME = "article.db";
    // @formatter:off
    public static final String SQL_CREATE_TABLE_ARTICLE = "CREATE TABLE IF NOT EXISTS "
            + ArticleColumns.TABLE_NAME + " ( "
            + ArticleColumns._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
            + ArticleColumns.TITLE + " TEXT NOT NULL, "
            + ArticleColumns.TITLE_DESCRIPTION + " TEXT, "
            + ArticleColumns.AUTHOR + " TEXT, "+
            " );";

在我实现内容提供商的NewsDetailActivity代码下面。

public class NewsDetailActivity extends AppCompatActivity {
    ImageButton addToFavoritesBtn;
   Article article;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.news_details);


        Toolbar myChildToolbar =
                (Toolbar) findViewById(R.id.detail_toolbar);
        setSupportActionBar(myChildToolbar);

        // Get a support ActionBar corresponding to this toolbar
        ActionBar ab = getSupportActionBar();
        ab.setDisplayHomeAsUpEnabled(true);

        // Enable the Up button

        getSupportActionBar().setDisplayShowTitleEnabled(false);

        final Article article = (Article) getIntent().getParcelableExtra("myDataKey");

        FloatingActionButton share = (FloatingActionButton)findViewById(R.id.share_fab);
        ImageButton shareButton = (ImageButton)findViewById(R.id.share_button);
        shareButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String articleDescription = article.getDescription() ;
                String articleTitle = article.getTitle();
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, articleDescription);
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, articleTitle);
                startActivity(Intent.createChooser(sharingIntent, "Share using"));
            }
        });

        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
                sharingIntent.setType("text/plain");
                String articleDescription = article.getDescription() ;
                String articleTitle = article.getTitle();
                sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, articleDescription);
                sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, articleTitle);
                startActivity(Intent.createChooser(sharingIntent, "Share using"));
            }
        });
        TextView textView = (TextView) findViewById(R.id.article_title);
        final String articleTitle = article.getTitle();

        if (articleTitle != null) {
            textView.setText(articleTitle);
        }
        TextView textView1 = (TextView) findViewById(R.id.article_author);
        final String articleAuthor = article.getAuthor();


        if (articleAuthor != null) {
            textView1.setText(articleAuthor);
        }


        TextView textView2 = (TextView) findViewById(R.id.article_body);
        String articleDescription = article.getDescription();
        if (articleDescription != null) {
            textView2.setText(articleDescription);

        }
        Button button = (Button) findViewById(R.id.article_url);
        final String articleUrl = article.getUrl();


        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent = new Intent(Intent.ACTION_VIEW,
                        Uri.parse(articleUrl));
                startActivity(intent);
            }
        });

        Picasso.with(this).load(article.getUrlToImage()).into((ImageView) findViewById(R.id.photo));

        addToFavoritesBtn = (ImageButton) findViewById(R.id.favorite_button);
        addToFavoritesBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ContentValues values = new ContentValues();
                values.put(ArticleColumns._ID, "INTEGER PRIMARY KEY AUTOINCREMENT"); //Value bro example 1
                values.put(ArticleColumns.TITLE, article.getTitle() ); // name
                values.put(ArticleColumns.TITLE_DESCRIPTION, article.getDescription());
                values.put(ArticleColumns.AUTHOR, article.getAuthor());

                getContentResolver().insert(ArticleColumns.CONTENT_URI, values);
            }
        });
    }
    //   String articlePublisheAt = article.getPublishedAt();
    // TextView textView3 = (TextView) findViewById(R.id.textPublisher);
    // if (articlePublisheAt != null) {
    //   textView3.setText(articlePublisheAt);
    //<TextView
    //     android:id="@+id/textPublisher"
    //   style="?android:attr/textAppearanceLarge"
    // android:layout_width="match_parent"
    // android:layout_height="wrap_content"
    // android:textColor="#fff"
    //android:textStyle="bold"
    //android:textSize="20sp"
    //android:lineSpacingMultiplier="0.9"/>

    //}


}

修改

删除 , 后,我现在收到以下错误: -

FATAL EXCEPTION: main Process: com.edgar.yodgorbekkomilo.newsapp, PID: 28122 android.database.sqlite.SQLiteDatatypeMismatchException: datatype mismatch (code 20) at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method) –

2 个答案:

答案 0 :(得分:3)

您应该删除另一个逗号(,),在该行的末尾:

ArticleColumns.AUTHOR + " TEXT, "

答案 1 :(得分:0)

我在create语句中看到一个额外的“,”。 CREATE TABLE IF NOT EXISTS文章(_id INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT NOT NULL,title_description TEXT,author TEXT, );