为什么Android上简单的SQLite应用程序不起作用?

时间:2017-05-29 01:18:38

标签: android sqlite

当我在模拟器上运行项目时,我收到一条消息“不幸的是,dezo12已停止”。

LogCat没有显示任何内容,我也尝试直接在智能手机上运行,​​但我的应用程序也崩溃了。

我使用eclipse neon,最近我在Android sdk manager中安装了所有需要的工具。

这是一段代码:

MAIN ACTIVITY.java

package com.example.dezo12;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView textView = (TextView)findViewById(R.id.textView1);

        SQLiteDatabase database = openOrCreateDatabase("rex.db", MODE_PRIVATE, null);
        database.execSQL("create table if not exist sampletable(name text, location text)");
        database.execSQL("insert into sampletable values('dezo', 'bela')");
        Cursor cursor = database.rawQuery("select * from sampletable", null);
        cursor.moveToFirst();
        String name = cursor.getString(0);
        String location = cursor.getString(1);
        textView.setText(name+ "/n" +location);


        database.close();

    }
}

ACTIVITY_MAIN xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${relativePackage}.${activityClass}" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="49dp"
        android:layout_marginTop="40dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.dezo12"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="22"
        android:targetSdkVersion="22" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

1 个答案:

答案 0 :(得分:1)

你写错了“存在”。像这样编辑这一行

database.execSQL("create table if not exists sampletable(name text, location text)");