首页 \ 问答 \ 如何为Sql lite数据库提供连接字符串?(How to give Connection String for Sql lite database?)

如何为Sql lite数据库提供连接字符串?(How to give Connection String for Sql lite database?)

首先,我想对于提出这个简单的问题表示歉意。 我正在尝试连接sql-lite数据库表。我正在使用以下代码。

package derb;
import java.sql.*;
public class db {
    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection con=DriverManager.getConnection("jdbc:sqlite:/home/anand/MySQLiteDB.TEST","root","ROOT");
        Statement st=con.createStatement();
        String q="insert into CHECKING values(3)";
        st.executeUpdate(q);
    }

}

TEST是我的数据库,我在eclipse中创建了一个名为CHECKING的表。但在上面的代码中给出了错误

Exception in thread "main" java.sql.SQLException: no such table: CHECKING
    at org.sqlite.DB.throwex(DB.java:288)
    at org.sqlite.NativeDB.prepare(Native Method)
    at org.sqlite.DB.prepare(DB.java:114)
    at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
    at derb.db.main(db.java:9)

我手动检查了eclipse表是否可用。但我不知道如何建立与该表的连接。所以任何人都可以帮我解决这个问题。

谢谢


First of all i would like to tell my apologies for asking this simple question. i am trying to connect sql-lite database table.I am using following code.

package derb;
import java.sql.*;
public class db {
    public static void main(String[] args) throws Exception {
        Class.forName("org.sqlite.JDBC");
        Connection con=DriverManager.getConnection("jdbc:sqlite:/home/anand/MySQLiteDB.TEST","root","ROOT");
        Statement st=con.createStatement();
        String q="insert into CHECKING values(3)";
        st.executeUpdate(q);
    }

}

TEST is my database and i created one table called CHECKING in eclipse.but in above code gives me the error

Exception in thread "main" java.sql.SQLException: no such table: CHECKING
    at org.sqlite.DB.throwex(DB.java:288)
    at org.sqlite.NativeDB.prepare(Native Method)
    at org.sqlite.DB.prepare(DB.java:114)
    at org.sqlite.Stmt.executeUpdate(Stmt.java:102)
    at derb.db.main(db.java:9)

I manually checked in the eclipse the table was available.but i don't know how to make the connection to that table.So any one can help me to fix this

Thank you


原文:https://stackoverflow.com/questions/34630193
更新时间:2022-02-12 16:02

最满意答案

首先,我们需要确保帖子有一个自定义的post_meta字段,您将在其上进行缩短。

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($post_id) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($post_id, $count_key, TRUE);
    if ($count == '') {
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

//Short code function
function wh_must_post_read() {
    $html = '';

    $args = [
        'meta_key' => 'whpp_track_post_views', //<--  Check This
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5
    ];

    $my_query_2 = new WP_Query($args);

    if ($my_query_2->have_posts()) :
        while ($my_query_2->have_posts()) : $my_query_2->the_post();

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('wh_must_post', 'wh_must_post_read');

用法
在PHP中

echo do_shortcode('[wh_must_post]');

在WP编辑器中

[wh_must_post]

请注意: 要查看此代码的实际操作,请访问几个帖子单页,以便whpp_set_post_views() mehord将whpp_track_post_views meta_key添加到该帖子。

以上所有代码都在您的活动子主题(或主题)的function.php文件中。 或者也可以在任何插件的php文件中。
代码经过测试和运行。

相关文章: 在没有插件的WordPress中按视图显示热门帖子
相关问题: 如何在wordpress中按日期而不是按日期更改帖子的顺序

希望这可以帮助!


First we need to make sure post has a custom post_meta fields on which you 'll be doing your shorting.

function whpp_track_post_views($post_id) {
    if (!is_single())
        return;
    if (empty($post_id)) {
        global $post;
        $post_id = $post->ID;
    }
    whpp_set_post_views($post_id);
}

add_action('wp_head', 'whpp_track_post_views');

function whpp_set_post_views($post_id) {
    $count_key = 'whpp_track_post_views';
    $count = get_post_meta($post_id, $count_key, TRUE);
    if ($count == '') {
        $count = 0;
        delete_post_meta($post_id, $count_key);
        add_post_meta($post_id, $count_key, '0');
    } else {
        $count++;
        update_post_meta($post_id, $count_key, $count);
    }
}

//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0);

//Short code function
function wh_must_post_read() {
    $html = '';

    $args = [
        'meta_key' => 'whpp_track_post_views', //<--  Check This
        'orderby' => 'meta_value_num',
        'order' => 'DESC',
        'posts_per_page' => 5
    ];

    $my_query_2 = new WP_Query($args);

    if ($my_query_2->have_posts()) :
        while ($my_query_2->have_posts()) : $my_query_2->the_post();

            $html .= "<p class=\"title\">" . get_the_title() . " </p>";
            $html .= "<p>" . get_the_excerpt() . "</p>";
            $html .= "<a href=\"" . get_permalink() . "\" class=\"readmore\">Read more</a>";

        endwhile;
        wp_reset_postdata();
    endif;
    return $html;
}

add_shortcode('wh_must_post', 'wh_must_post_read');

USAGE
In PHP

echo do_shortcode('[wh_must_post]');

In WP Editor

[wh_must_post]

Please Note: To see this code in action visit few post single page so that whpp_set_post_views() mehord will add whpp_track_post_views meta_key to that post.

All the above code goes in function.php file of your active child theme (or theme). Or also in any plugin php files.
Code is tested and works.

Related Article: Display Popular Posts by Views in WordPress without a Plugin
Related Question : How to change the order of posts by number of views not by date in wordpress

Hope this helps!

相关问答

更多

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。