YXTS122
2016-12-15 01:10
采纳率: 100%
浏览 1.1k

安卓,创建数据库时崩溃了

DatabaseHelper.java

 package com.exampl.mysql;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DatabaseHelper extends SQLiteOpenHelper{
    private Context mContext;
    public static final String CREATE_TABLE_SQL="create table dict(_id integer primary key autoincrement,word text,detail text);";

    public DatabaseHelper(Context context,String name,CursorFactory factory,int version)
    {
        super(context,name,factory,version);
        mContext=context;
    }

    public void onCreate(SQLiteDatabase db)
    {
        db.execSQL(CREATE_TABLE_SQL);
        Toast.makeText(mContext,"创建成功!",Toast.LENGTH_LONG).show();
    }

    public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
    {

    }


}

MainActivity.java

 package com.exampl.mysql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{
    private Button insert=null;
    private Button search=null;
    private EditText word=null;
    private EditText detail=null;
    private EditText key=null;
    DatabaseHelper dbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        dbHelper=new DatabaseHelper(this,"myDict.db3",null,1);
        insert=(Button)findViewById(R.id.insert);
        insert.setOnClickListener(this);
        search=(Button)findViewById(R.id.search);
        search.setOnClickListener(this);
        word=(EditText)findViewById(R.id.word);
        detail=(EditText)findViewById(R.id.detail);
        key=(EditText)findViewById(R.id.key);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.insert:
            String wor=word.getText().toString().trim();
            String det=detail.getText().toString().trim();
            insertData(dbHelper.getReadableDatabase(),wor,det);
            Toast.makeText(this, "添加生词成功!", Toast.LENGTH_LONG).show();
            break;
        case R.id.search:
            String ke=key.getText().toString().trim();
            Cursor cursor=dbHelper.getReadableDatabase().rawQuery("select*from dict where word like? or details like?", new String[]{"%"+ke+"%","%"+ke+"%"});
            Bundle data=new Bundle();
            data.putSerializable("date", converCursorToList(cursor));
            Intent intent=new Intent(MainActivity.this,Result.class);
            intent.putExtras(data);
            startActivity(intent);
            break;
        default:break;
        }
    }

    protected ArrayList<Map<String,String>> converCursorToList(Cursor cursor)
    {
        ArrayList<Map<String,String>> result=new ArrayList<Map<String,String>>();
        while (cursor.moveToNext())
        {
            Map<String,String> map=new HashMap<String,String>();
            map.put("word", cursor.getString(1));
            map.put("detail", cursor.getString(2));
            result.add(map);
        }
        return result;
    }

    private void insertData(SQLiteDatabase db,String word,String detail)
    {
        db.execSQL("insert into dict values(null,?,?)"+new String[]{word,detail});
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        if (dbHelper!=null)
            dbHelper.close();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

Result.java

 package com.exampl.mysql;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class Result extends Activity {
    private TextView jieguo=null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.line);
        jieguo=(TextView)findViewById(R.id.jieguo);
        Bundle bundle=getIntent().getExtras();
        jieguo.setText(bundle.getString("date"));
    }

}

图片说明
图片说明
图片说明

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

6条回答 默认 最新

  • stone_you 2016-12-15 10:09
    已采纳

    改成这样应该可以了
    db.execSQL("insert into dict (word, detail) values(?,?)", new String[]{word,detail});
    字符串这么拼是不对的,new String[]{word,detail}是一个对象,拼出来的不是sql语句

    打赏 评论
  • YXTS122 2016-12-15 01:24

    activity_main.xml

     <LinearLayout 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"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.exampl.mysql.MainActivity"
        android:orientation="vertical" >
    
        <EditText
            android:inputType="text"
            android:id="@+id/word"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <EditText
            android:inputType="text"
            android:id="@+id/detail"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:lines="3" />
        <Button
            android:id="@+id/insert"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/insert" />
        <EditText
            android:inputType="text"
            android:id="@+id/key"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <Button
            android:id="@+id/search"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/search" />
        <ListView 
            android:id="@+id/show"
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
        </ListView>
    
    
    </LinearLayout>
    
    

    line.xml

     <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
    
        <TextView
            android:id="@+id/jieguo"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    
    
    </LinearLayout>
    
    

    AndroidManifest.xml

     <?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.exampl.mysql"
        android:versionCode="1"
        android:versionName="1.0" >
    
        <uses-sdk
            android:minSdkVersion="8"
            android:targetSdkVersion="21" />
    
        <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>
    
            <activity
                android:name=".Result"
                android:label="@string/app_name" >
            </activity>
    
        </application>
    
    </manifest>
    
    
    打赏 评论
  • 梦想家丶Kern 2016-12-15 01:41

    这是SQL插入报异常。SQL语句报错一般不会明确问题出在哪里。需要仔细排查一下。看Log日志,应该是insert into dict 这里出问题了,检查一下:1.数据类型是否正确;2.是否插入了null;2.数据库中字段是否是SQL内定的字段(比如index,被数据库占用了,如果你在使用,会报各种奇葩的错,需要转义)。

    打赏 评论
  • DarrenHang.L 2016-12-15 01:49

    db.execSQL("insert into dict values(null,?,?)"+new String[]{word,detail}); 这句话错了,你的主键已经自动增长,主键不用自己添加,你添加会报错的,

    打赏 评论
  • DarrenHang.L 2016-12-15 01:54

    你把null去掉就可以了

    打赏 评论
  • Yang_Farley 2016-12-15 11:42

    db.execSQL("insert into dict values(null,?,?)"+new String[]{word,detail}); 你拿三个值给两个列赋值 能不崩溃么?

    打赏 评论

相关推荐 更多相似问题